Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 35 of 35

Thread: Help writing to a file getting an exception error

  1. #26
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help writing to a file getting an exception error

    Yes, the problem is with the readFile() method. All the other code works perfectly. I am unsure on how to read data and store the data into variables. I thought the scanner method could achieve that result. I do not understand on how to read each line of code store it into a variable successfully. That is my problem. Can you explain this to me or point me into direction where I can learn.

  2. #27
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help writing to a file getting an exception error

    Again, this method should not be part of the Player class but rather some utility class that has a readPlayer(String name) method, and that returns either a Player object if the file is found or null or throws an exception if no player file is found. What the method should do is read in the data, create a new Player object with the data and the Player constructor, and then return that Player, simple as that. You could use serialization if you so desire to achieve this, or you could use a plain text file if you want the data to be humanly readable (I won't even go into the other options such as database or JAXB right now). The method could even be a static utility method since it really won't have a state.

    As for your Scanner object, you seem to be opening and closing the same Scanner variable all over the place. I would simplify this greatly. Just get rid of any class-level Scanner variables, and only create declare a Scanner variable and create its object once at the beginning of a method that needs to use it, and close it at the end of the method. This is part of the KISS philosophy of programming: Keep It Simple Stupid! So in other words, get rid of your Scanner sn variable that you declare at the top of your class and instead create a new variable only inside of methods that need it.
    Last edited by curmudgeon; September 29th, 2012 at 10:15 PM.

  3. #28
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help writing to a file getting an exception error

    Thank you, Thank you, and Thank you.

  4. #29
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help writing to a file getting an exception error

    Quote Originally Posted by loui345 View Post
    Thank you, Thank you, and Thank you.
    You're welcome, and best of luck!

  5. #30
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help writing to a file getting an exception error

    It worked... I had to many scanners opening and not closing.

  6. #31
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help writing to a file getting an exception error

    Wonderful! Glad you've got it solved!

  7. #32
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help writing to a file getting an exception error

    Even though it is solved, do you think i should still rewrite the code and make it like you suggested? It seems like a lot of extra work, but do you think it will pay dividends in the future?

  8. #33
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help writing to a file getting an exception error

    Quote Originally Posted by loui345 View Post
    Even though it is solved, do you think i should still rewrite the code and make it like you suggested? It seems like a lot of extra work, but do you think it will pay dividends in the future?
    It depends. If this is for a homework assignment, and your work is now complete, then perhaps what you've done is enough, but going forward with new projects I would certainly do this. Again, if I had a Player object it would contain code that allowed me to set and get the "state" of a player, an could have certain player like behaviors, like placeABet() or takeATurn() (if this was a game). I would have a GamePiece classes as needed such as Card and Deck if playing cards, or ChessPiece, ChessBoard, and specific ChessPiece child classes if chess, I would have a Game class that controlled everything. I would have file I/O in a separate class. I would not have any "user interaction" code inside of the core classes such as Player or a game piece class, but rather that would somehow be incorporated in the Game. The idea is to make classes with high "cohesion" -- to have classes where states and behaviors that belong together are in the same class, but with loose "coupling" -- where classes don't handle behaviors that aren't an essential part of it.

  9. #34
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Help writing to a file getting an exception error

    Can someone close this it's solved.

  10. #35
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help writing to a file getting an exception error

    Quote Originally Posted by loui345 View Post
    Can someone close this it's solved.
    There are instructions on closing the thread in the Announcements thread that says Welcome! If it's your first time here, please read.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. counting the values in input file and and writing the output to a file
    By srujirao in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 8th, 2012, 02:48 PM
  2. new line in file writing
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 30th, 2012, 09:06 AM
  3. Error writing to an array with the static modifier.
    By handuel in forum Collections and Generics
    Replies: 2
    Last Post: December 25th, 2011, 10:10 AM
  4. Need Help Writing Exception code.
    By USCPaddler in forum Exceptions
    Replies: 4
    Last Post: November 10th, 2011, 05:45 PM
  5. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM