Results 1 to 5 of 5

Thread: Problem in java

  1. #1
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default 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.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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 ;
    	}
    Should align the score ..
    dont worry newbie, we got you covered.

  3. #3
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default

    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.

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    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.

  5. #5
    mcal is offline Member
    Join Date
    Jan 2008
    Posts
    39
    Rep Power
    0

Similar Threads

  1. Problem to ToolTip in Java 3d
    By roshithmca in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 04:46 AM
  2. Problem in java
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-09-2008, 05:13 PM
  3. Problem with timer in java
    By paul in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2007, 11:18 AM
  4. JAVA if problem
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:58 PM
  5. java SE 6 problem
    By techlance in forum Java Applets
    Replies: 1
    Last Post: 06-28-2007, 11:10 AM

Posting Permissions

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