Question

I have been stuck on this problem for so long and i have no idea what to do. Basically i have a text file with people names then student number then prize money like this:

Green%3243%1000
Kevin%7657%400
Frank%345%10000
Bob%5435%5000
Stefan%31231%1000
Javis%4532%100
IronMan%5435%2000
Lamp%534%3000

What i want to be able to do is sort the array based on the last number.

I tried this abomination (Don't bother reading it its garbage):

    boolean flag = true;
    String temp;
    int temp1;
    int temp2;

    while (flag){
        flag = false;
        for(int j=0;  j < list.size() -1;  j++ ){
            System.out.println(list.get(j));
            Scanner s = new Scanner(list.get(j)).useDelimiter("%");
            s.next();
            s.next();
            temp1 = s.nextInt();
            Scanner s2 = new Scanner(list.get(j+1)).useDelimiter("%");
            s2.next();
            s2.next();
            temp2 = s2.nextInt();
            if (temp1 < temp2){
                temp = list.get(j);
                list.add(j, list.get(j+1));
                list.add(j+1,temp);
                flag = true;
            } 
        } 
    }

But its just infinitely looping. My though while making it was just patching array lists into a bubble sort.

If anyone has any ideas and is willing to share them it will be greatly appreciated.

Was it helpful?

Solution

Java is an object-oriented language, so I'll just use objects:

  • Create a Student object with the three values you want to store (and a toString() method to print them separated by "%":

    public class Student {
        private final String name;
        private final int number;
        private final int prizeMoney;
    
        public Student(final String name, final int number, final int prizeMoney) {
            this.name = name;
            this.number = number;
            this.prizeMoney = prizeMoney;
        }
    
        @Override
        public String toString() {
            return name+"%"+number+"%"+prizeMoney;
        }
    
        public int getPrizeMoney() {
            return prizeMoney;
        }
    }
    
  • Read your lines as Student objects, and store them in a List:

    final Scanner scan = new Scanner(new File("/path/to/StudentsList"));
    final List<Student> students = new ArrayList<Student>();    
    
    while (scan.hasNextLine()) {
        final Scanner line = new Scanner(scan.nextLine());
        line.useDelimiter("%");
    
        students.add(new Student(line.next(), line.nextInt(), line.nextInt()));
    
        line.close();
    }
    scan.close();
    
  • Order the List with a custom Comparator, and print it:

    students.sort(new Comparator<Student>() {
        @Override
        public int compare(final Student s1, final Student s2) {
            return s1.getPrizeMoney()-s2.getPrizeMoney();
        }
    });
    
    for (final Student student: students)
        System.out.println(student);
    

Output:

Javis%4532%100
Kevin%7657%400
Green%3243%1000
Stefan%31231%1000
IronMan%5435%2000
Lamp%534%3000
Bob%5435%5000
Frank%345%10000

OTHER TIPS

Here's something for you to get head started.

  1. Create a map for prize money => line as key/value pair
  2. Read each line in the file, parse it and put key/value pair in the above map
  3. Once your map is ready, convert the keys entry set into the collections like list
  4. Sort the collections, using Collections.sort()
  5. Iterate over the created map, and for each value in the collection get the corresponding value from the map.

Hope this helps you to get the workflow.

Id consider creating a 3d array here 8x8x8 from right to left in the array is row, col, and in so [0][0][1] is block one or kevin [0][1][1] is 7657 [1][1][1] is 400. I like this way because not only does it give each 'item' an array it allows you to keep it organized and easily accessable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top