Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2008, 10:17 PM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
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:
for(int j = 0; j < questions.length; j++) { int randomIndex = B[j]; String input = JOptionPane.showInputDialog(null, questions[randomIndex]); if(answers[randomIndex].equalsIgnoreCase(input)) { count++; // incrementing counter if entered answer is correct point++; correct++; JOptionPane.showMessageDialog(null, "Congratulations that is correct!"); } else { JOptionPane.showMessageDialog(null, "Wrong Answer!"); } if(!timeForMore) // if time is over, the program executes the loop an stops asking questions. break; } JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); int percent = (count*100)/questions.length; JOptionPane.showMessageDialog(null, "Your Geography Quiz score is " + point + " % "); try { BufferedWriter out; String name = JOptionPane.showInputDialog(null, "Enter your name"); String type = JOptionPane.showInputDialog(null, "Enter your quiz type"); if(type.equals("Plate Tectonics")){ out = new BufferedWriter(new FileWriter("players.txt",true)); out.write(name); //Writing to the textfile (the name entered by user) out.write(getSpace(20- name.length())); // Here 20 is max length possible change accordingly String trimmedStr = name.trim(); out.write(String.valueOf(points+ " % ")); 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.length-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; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-14-2008, 09:13 AM
Member
 
Join Date: Jan 2008
Posts: 39
mcal is on a distinguished road
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(); }
Thanks a lot.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting problem... mark-mlt New To Java 4 04-17-2008 03:15 PM
Problem with sorting Table sireesha264 Advanced Java 0 02-08-2008 03:21 PM
Sorting JTable (Vectors) Problem ramapple AWT / Swing 5 02-05-2008 09:54 AM
Heap Sorting kesav2005 New To Java 1 11-13-2007 05:04 PM
sorting JTable mansi_3001 Advanced Java 3 08-10-2007 07:29 PM


All times are GMT +3. The time now is 05:17 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org