Results 1 to 13 of 13
  1. #1
    omfgimawesome is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default [Help Please] Java [Rsps Command]

    Java Code:
    if (playerCommand.startsWith("lottery")) {
       try {
       BufferedWriter report = new BufferedWriter(new FileWriter("./Data/Report/Report.txt", true));
       try {
    	report.write(c.number + ". " + c.playerName);
            c.number += 1;
    	report.newLine();
    	} finally {
    	report.close();
    }
    	} catch (IOException e) {
                  e.printStackTrace();
    	}
    }
    I basically need help with one thing.
    I want it to says the following (when using the ::report command).
    1. Player name.
    2. Player name.
    3. Player name.
    etc..so the numbers will be in right order, however the value of the integer doesn't save,
    and it looks like this:


    Java Code:
    1. Player name.
    1. Player name.
    Please help! Much appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't tell where your error is based on the code posted. It does seem a bit unusual though that you're directly manipulating a class's field, number rather than using a setter variable and rather than using an array or collection.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You havent really shown enough to know, my guess is that c is getting re initialized to a start value each time through the loop.

  4. #4
    omfgimawesome is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    c is a class called Player.
    There's no reror, what I mean is:
    When you type ::report, your name will be send to class named 'Report'.
    And it will look like this:
    Java Code:
    1. Player
    report.write(c.number + ". " + c.playerName);
    As you can see above ^
    But, if I type the command again, instead of showing this:
    Java Code:
    2. Player
    It will show this again:
    Java Code:
    1. Player

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,537
    Blog Entries
    7
    Rep Power
    20

    Default

    A very stupid way to do it would be to read the number of lines already present in the file, say 'n', so that the next name to be written has the numerical value n+1. But I'd rather keep track of that number elsewhere ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    omfgimawesome is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    A very stupid way to do it would be to read the number of lines already present in the file, say 'n', so that the next name to be written has the numerical value n+1. But I'd rather keep track of that number elsewhere ...

    kind regards,

    Jos
    And how I can make it read the line exactly? Thanks for the help.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also I agree with fubar, you probably shouldn't access variables in a class directly. Would you mind showing us some more code? Are you using a loop? Is this all happening upon execuing the code once?

  8. #8
    omfgimawesome is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Well this is all of the code, which located at commands.java
    At player java there's the integer "public int number = 1;"

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    That can't be all the code, for instance; your snippet doesn't show when c was initialized.

  10. #10
    omfgimawesome is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    That can't be all the code, for instance; your snippet doesn't show when c was initialized.
    c means Player.java which is a class..
    And yes this is all of the code..:\

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I understand that, but you can't just use an object without previously declaring an instance of the object.

    Java Code:
    public class X{}
    public class Y{
      public static void main(String[] args){
        System.out.println(z);
      }
    }
    Z is an instance of X, however; I never declared or initialized z so this code wont compile.

  12. #12
    omfgimawesome is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I understand that, but you can't just use an object without previously declaring an instance of the object.

    Java Code:
    public class X{}
    public class Y{
      public static void main(String[] args){
        System.out.println(z);
      }
    }
    Z is an instance of X, however; I never declared or initialized z so this code wont compile.
    Well that's what the command is for You help a lot but you still don't get what I need help with.
    Basically just counting every line, like the other guy's problem (count worlds).

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I was trying to see more of the code to help get an understanding of the problem.

    To clarify jos's point, you can simply read in one line at a time from the file and increment a counter as you go, this will count the number of lines in a file and you can write to n+1

    Java Code:
    int j;
    for(String s, j = 1; (s=in.readLine() != null); j++)
      ;
    Something like this should work to read through a file and count the lines in it. At the end j will be the length of the file, so you will want to write j+1

Similar Threads

  1. How do you use the Mac command line for Java?
    By java beginner in forum New To Java
    Replies: 16
    Last Post: 05-07-2011, 04:34 AM
  2. Letting my program command the command prompt
    By Smokey. in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-29-2011, 11:34 PM
  3. Help with JAVA and command promt!
    By Sinfonian22291 in forum New To Java
    Replies: 5
    Last Post: 03-05-2011, 04:35 PM
  4. Java command line
    By agouzoul in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 12:12 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 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
  •