Results 1 to 3 of 3
  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Question Sorting Elements From a File

    Hello!

    I put some names and scores into a file called "hej.txt", like this:

    Random1
    10
    Random2
    20
    Random3
    30
    etc.
    Then I made a program that read the contents of the file and output the biggest score and the name of the person who performed the score. However, my program doesn't seem to sort the scores, since they're in the same order as they were before sorting. Do you know why this happens, and how to get it working? This is my code:

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Ostbågar{
        public static void main(String[] args){
            File file = new File("hej.txt");
            try{
                Scanner hej = new Scanner(file);
                int rader = 0;
                while(hej.hasNext()){
                    String s = hej.nextLine();
                    if(s == null)
                        break;
                    rader++;
                }
                hej = new Scanner(file);
                if(rader > 1 && rader % 2 == 0){
                    String namn[] = new String[rader/2];
                    int scores[] = new int[rader/2], x = 0, y = 0;
                    while(hej.hasNext()){
                        String s = hej.nextLine();
                        if(s == null)
                            break;
                        namn[x] = s;
                        s = hej.nextLine();
                        scores[y] = Integer.parseInt(s);
                        x++; y++;
                    }
                    while(x<(scores.length-1)){
                        for(y=0;y<scores.length-1;y++){ // SORTING THE ELEMENTS HERE!
                            if(scores[y] < scores[y+1]){
                                int temp = scores[y];
                                scores[y] = scores[y+1];
                                scores[y+1] = temp;
                                String grej = namn[y];
                                namn[y] = namn[y+1];
                                namn[y+1] = grej;
                            }
                        }
                        x++;
                    }
                    System.out.print(namn[0] + " had the highest score with " + scores[0] + "!");
                }else
                    System.out.print("Error reading file!");
            }catch(FileNotFoundException e){  
            }
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: Sorting Elements From a File

    I'm not certain I understand. How do you know your sort doesn't work if you already put them in sorted order? Or am I not understanding something?

    Edit: Ok, after looking at your code, you need to reset x to 0 before your while statement of your sort loop.

    Regards,
    Jim
    Last edited by jim829; 10-01-2013 at 08:22 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    152
    Rep Power
    1

    Default Re: Sorting Elements From a File

    Oh, of course, how did I not see that? Thanks :)

Similar Threads

  1. Writing the elements of a TreeMap to a text file
    By busdude in forum New To Java
    Replies: 0
    Last Post: 10-03-2011, 03:05 AM
  2. Swapping/Sorting elements in an Array
    By kumalh in forum New To Java
    Replies: 2
    Last Post: 08-10-2011, 04:52 AM
  3. sorting data in txt file
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 03:02 AM
  4. Sorting xml file
    By keioGirl in forum Advanced Java
    Replies: 4
    Last Post: 12-12-2008, 05:14 PM
  5. Sorting Elements in a TreeMap
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •