sorting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    sorting problem

    I've got a problem in sorting. I have to use a bubble sort to sort the scores with those who bring the highest percent at the top. The scores of each user i saved in a text file. This is how i tried to do the sorting but its not working. Can someone pls help me out. Thanks a lot.


    [CODE=java]for(int j = 0; j < questions.lengt h; j++) {


    int randomIndex = B[j];
    String input = JOptionPane.sho wInputDialog(nu ll, questions[randomIndex]);


    if(answers[randomIndex].equalsIgnoreCa se(input))
    {
    count++; // incrementing counter if entered answer is correct
    point++;

    correct++;
    JOptionPane.sho wMessageDialog( null, "Congratulation s that is correct!");
    }

    else
    {

    JOptionPane.sho wMessageDialog( null, "Wrong Answer!");
    }

    if(!timeForMore ) // if time is over, the program executes the loop an stops asking questions.
    break;
    }


    JOptionPane.sho wMessageDialog( null, "You answered " + count +
    " out of " + questions.lengt h +
    " questions correctly.");
    int percent = (count*100)/questions.lengt h;
    JOptionPane.sho wMessageDialog( null, "Your Geography Quiz score is " + point + " % ");




    try {
    BufferedWriter out;
    String name = JOptionPane.sho wInputDialog(nu ll, "Enter your name");
    String type = JOptionPane.sho wInputDialog(nu ll, "Enter your quiz type");




    if(type.equals( "Plate Tectonics")){
    out = new BufferedWriter( new FileWriter("pla yers.txt",true) );



    out.write(name) ; //Writing to the textfile (the name entered by user)

    out.write(getSp ace(20- name.length())) ; // Here 20 is max length possible change accordingly
    String trimmedStr = name.trim();
    out.write(Strin g.valueOf(point s+ " % "));
    out.write(" ");
    out.write(type) ;
    out.newLine(); //write a new line to the textfile so the next time it writes to the file it does it on the next line
    out.close();


    for(int i=1; i<points.length ; i++) {
    for(int j=0; j<points.lengt h-i; j++) { //if in wrong order then swap
    if (points[j]>points[j+1]) swap(points, j, j+1);
    }
    }


    }
    private static void swap (int[] points, int i, int j) {
    int h = points[i];
    points[i] = points[j];
    points[j] = h;
    }

    [/CODE]
    Last edited by Ganon11; Feb 13 '08, 10:26 PM. Reason: Changing [code] tag to [code=java] tag
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Is this an assignment? Do you have to code your own sort or can you simplify your life and use the sort methods of the API?

    Comment

    • saytri
      New Member
      • Dec 2007
      • 35

      #3
      Yes its an assignment. I have to use a bubble sort. I tried doing it but its not sorting the scores.

      Comment

      • saytri
        New Member
        • Dec 2007
        • 35

        #4
        My problem is that the program isn't sorting the high scores. In my program according to my tutor i have to use a bubble sort. The program is cpompiling and it is displaying the score. The problem is that its not sorting them. I think the problem is because i'm only passing one number to the array everytime a user does his turn in the game. The scores one scored are written directly into a text file. Therefore my problem is how to sort these numbers since they are stored in a textfile. Pls i really am lost. I really appreciate any help. Thanks a lot.

        This is the code:

        Code:
        if(type.equals("Car")){
                      out = new BufferedWriter(new FileWriter("players.txt",true));
                       
                     int a[] = {percent}; //what do i need to pass here so the array can read all the scores that are stored in the textfile together with the new                score.
                     int i;
         
         
                     int temp, counter, index;
         
                     int length=0;
         
                     for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
         
                       for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
         
                        if(a[index] > a[index+1]) { //Test if need a swap or not.
         
                            temp = a[index]; //These three lines just swap the two elements:
         
                           a[index] = a[index+1];
         
                            a[index+1] = temp;
         
                        }
         
                    }
         
                }
                    
                     
                     out.write(String.valueOf(percent));
         
                   
                     out.newLine(); 
                     out.close();
                    }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Have you read this article first?

          Comment

          • saytri
            New Member
            • Dec 2007
            • 35

            #6
            I have read it now. Thanks a lot. But the problem still remanains. Since i have to pass not a set of numbers to the array, but every score that the user scores, which is stored in the textfile. What do i have to pass to the array so it would read all the scores that are stored in the file. Thanks again.

            Comment

            Working...