Results 1 to 5 of 5
Thread: Problem in java
- 01-14-2008, 10:24 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
Problem in java
I've got a problem with a score list. I am diplaying the scores of all the users who enter the game. The problem is that the score are not listed in an alligned form. The player names are displayed in a good way, but the scores are either displayed as only one word (i.e the score joined with the word example John90 or else when i tried to include a space between the name and the score, the scores are not displayed in an alligned form (they depend on the length of the name) eg:
John 90
Raymond 80
Jake 10
Charmaine 90
This is the piece of code used to display this (this is being displayed on a dialog box, and the text is called from a textfile to a TextArea):
Java Code:try { JOptionPane.showMessageDialog(null, "You answered " + count + " out of " + questions.length + " questions correctly."); BufferedWriter out; String text = JOptionPane.showInputDialog(null, "Enter your name"); out = new BufferedWriter(new FileWriter("players.txt",true)); out.write(text);//Write out a string to the text file out.write(" "); // i tried to use this to make a space, but its making a space depending on the length of the player name out.write(String.valueOf(count)); out.newLine(); out.close(); }catch(IOException e){ System.out.println("There was a problem:" + e); }
Thanks for the help.
- 01-15-2008, 02:10 AM #2
Should align the score ..Java Code:// Instead of :: out.write(" "); // i .. out.write(getSpace(20- text.length())); // Here 20 is max length possible change accordingly ... public static String getSpace(int length){ String str = ""; for(int i = 0 ; i < length ; i++ ){ str = str + " " ; } return str ; }dont worry newbie, we got you covered.
- 01-15-2008, 03:48 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
Hi thanks a lot for your response. I have understood what you have written, but it seems that its doing the same thing (i.e not alligning the score).
Thanks.
- 01-15-2008, 09:04 PM #4
you can apply trim() to player's name .. and above code should work provided that the you use maximum possible length of name instead of 20.
Delete the existing data file as well.dont worry newbie, we got you covered.
- 01-16-2008, 10:09 PM #5
Member
- Join Date
- Jan 2008
- Posts
- 39
- Rep Power
- 0
Similar Threads
-
Problem to ToolTip in Java 3d
By roshithmca in forum AWT / SwingReplies: 1Last Post: 02-05-2008, 03:46 AM -
Problem in java
By saytri in forum New To JavaReplies: 6Last Post: 01-09-2008, 04:13 PM -
Problem with timer in java
By paul in forum Advanced JavaReplies: 3Last Post: 07-26-2007, 10:18 AM -
JAVA if problem
By toby in forum New To JavaReplies: 2Last Post: 07-25-2007, 07:58 PM -
java SE 6 problem
By techlance in forum Java AppletsReplies: 1Last Post: 06-28-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks