473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sorting problem

35 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. for(int j = 0; j < questions.length; j++) {
  2.  
  3.  
  4.            int randomIndex = B[j];
  5.           String input = JOptionPane.showInputDialog(null, questions[randomIndex]); 
  6.  
  7.  
  8.           if(answers[randomIndex].equalsIgnoreCase(input))
  9.           { 
  10.            count++;  // incrementing counter if entered answer is correct
  11.            point++;
  12.  
  13.            correct++;
  14.            JOptionPane.showMessageDialog(null, "Congratulations that is correct!");
  15.            }
  16.  
  17.            else
  18.             {
  19.  
  20.                 JOptionPane.showMessageDialog(null, "Wrong Answer!");
  21.             }
  22.  
  23.     if(!timeForMore) // if time is over, the program executes the loop an stops asking questions.
  24.            break; 
  25.      }
  26.  
  27.  
  28.             JOptionPane.showMessageDialog(null, "You answered " + count +
  29.                                       " out of " + questions.length +
  30.                                       " questions correctly.");
  31.             int percent = (count*100)/questions.length;
  32.         JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + point + " % ");
  33.  
  34.  
  35.  
  36.  
  37.              try {
  38.               BufferedWriter out;
  39.               String name = JOptionPane.showInputDialog(null, "Enter your name");
  40.               String type = JOptionPane.showInputDialog(null, "Enter your quiz type");
  41.  
  42.  
  43.  
  44.  
  45.              if(type.equals("Plate Tectonics")){
  46.                   out = new BufferedWriter(new FileWriter("players.txt",true));
  47.  
  48.  
  49.  
  50.              out.write(name); //Writing to the textfile (the name entered by user)
  51.  
  52.              out.write(getSpace(20- name.length())); // Here 20 is max length possible change accordingly
  53.              String trimmedStr = name.trim();
  54.              out.write(String.valueOf(points+ " % "));
  55.              out.write("         ");
  56.              out.write(type);
  57.              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
  58.              out.close();
  59.  
  60.  
  61.                for(int i=1; i<points.length; i++) { 
  62.                for(int j=0; j<points.length-i; j++) { //if in wrong order then swap  
  63.                 if (points[j]>points[j+1]) swap(points, j, j+1); 
  64.  
  65.  
  66.         }
  67. private static void swap (int[] points, int i, int j) { 
  68.      int h = points[i]; 
  69.      points[i] = points[j]; 
  70.      points[j] = h; 
  71.     }
  72.  
  73.  
Feb 13 '08 #1
5 1292
BigDaddyLH
1,216 Recognized Expert Top Contributor
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?
Feb 13 '08 #2
saytri
35 New Member
Yes its an assignment. I have to use a bubble sort. I tried doing it but its not sorting the scores.
Feb 13 '08 #3
saytri
35 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. if(type.equals("Car")){
  2.               out = new BufferedWriter(new FileWriter("players.txt",true));
  3.  
  4.              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.
  5.              int i;
  6.  
  7.  
  8.              int temp, counter, index;
  9.  
  10.              int length=0;
  11.  
  12.              for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.
  13.  
  14.                for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.
  15.  
  16.                 if(a[index] > a[index+1]) { //Test if need a swap or not.
  17.  
  18.                     temp = a[index]; //These three lines just swap the two elements:
  19.  
  20.                    a[index] = a[index+1];
  21.  
  22.                     a[index+1] = temp;
  23.  
  24.                 }
  25.  
  26.             }
  27.  
  28.         }
  29.  
  30.  
  31.              out.write(String.valueOf(percent));
  32.  
  33.  
  34.              out.newLine(); 
  35.              out.close();
  36.             }
  37.  
Feb 14 '08 #4
r035198x
13,262 MVP
Have you read this article first?
Feb 14 '08 #5
saytri
35 New Member
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.
Feb 14 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
4
by: John Bullock | last post by:
Hello, I am at wit's end with an array sorting problem. I have a simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
7
Plater
by: Plater | last post by:
I am having trouble determining when my DataGridView object is sorting (based on a column header click). The idea is, in a large table, sorting the columns takes time, so I show a splash screen....
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.