Results 1 to 7 of 7
  1. #1
    Maretaga is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Problem with the Scanner

    Hi,

    For an assignment, I have to use the Scanner-object to read information from a .txt-file. However, it's acting weird. The .txt-file starts with:
    Simple_Pieces
    ==========
    A RED 1

    The bit of code that is acting weird:
    Java Code:
    public BagOfPieces(File pieces) throws FileNotFoundException {
            try{
                Scanner scan = new Scanner(pieces);
                name = scan.next();
                scan.next();                            //Skip the "============"-part
    Now, if I'm not mistaken, the "name = scan.next()"-part should read "Simple_pieces", which should then be assigned to the variable "name" (name is a String that has been declared elsewhere). Then, the next "scan.next()" should read the "==========="-line and do nothing else with it (if I'm not mistaken).
    However, when I debug this part and it enters the breakpoint at the 4th line of the code above, it says that "scan.next()" is "Simple_Pieces". When I continue, moving on to the breakpoint in the 5th line, the debugger tells me that "name" has the value "============".

    Could someone explain me why this isn't working they way I'd like it?

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

    Default

    Try printing name after assigning it. Are you saying that name becomes "======="? are you looping and changing name to scan.next()? I ask this because scan.next() reads a token, not a line. It reads from the first non whitespace to the last non white spaces(don't forget new line, so the second call to scan.next() may just be reading the new line character.

  3. #3
    Maretaga is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    I tried printing "name" and this is where the real fun begins.
    When I simply run it, the code prints "Simple_Pieces".
    When I debug it, the code prints either "==========" or "Simple_Pieces" (that is, sometimes it screws up, sometimes it doesn't).

    Although this pretty much solves the problem, this is a bit odd O.o Any idea on what causes this?

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

    Default

    Try using nextLine() instead of next(), I'd imagine the first call the next() get the string Xxxx_Xxxx, and the second call simply swallows the new line character.

  5. #5
    Maretaga is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    I think I have found the problem. Checking the value of "scan.next()" by holding your mouse over it messes things up. Thanks for the help ^^

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Maretaga View Post
    When I debug it, the code prints either "==========" or "Simple_Pieces" (that is, sometimes it screws up, sometimes it doesn't).
    I find that hard to believe. The same code snippet will not behave differently for the same set of data.

    As sunde has mentioned you probably want to use nextLine() as next() will only give you the next token/word.

  7. #7
    Maretaga is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    I find that hard to believe. The same code snippet will not behave differently for the same set of data.
    That's what I thought, but apparantly the debugger has a few issues when it comes to checking scan.next() while it runs. So if any of you ever runs into this problem... ^^

Similar Threads

  1. Scanner problem.
    By keo in forum New To Java
    Replies: 6
    Last Post: 04-07-2011, 08:30 AM
  2. Scanner problem
    By mkay in forum New To Java
    Replies: 2
    Last Post: 10-28-2010, 12:09 AM
  3. Problem with scanner
    By JavaJ in forum New To Java
    Replies: 6
    Last Post: 03-16-2010, 08:51 PM
  4. Problem with scanner
    By Kangaroo128 in forum New To Java
    Replies: 11
    Last Post: 09-01-2009, 08:07 AM
  5. Problem with scanner in loop
    By grifan526 in forum New To Java
    Replies: 2
    Last Post: 07-21-2009, 12:06 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
  •