Results 1 to 6 of 6
  1. #1
    ScKaSx is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Arrow Using scanner.hasNext() but recognize return

    Hi All,

    I have the following file I am trying to read in with scanner:

    a b c d
    a b c
    a b c d
    a b c d

    As I read in I want to place the a's in one part of an object, the b's in another and so on. The trick is, if d is not there then I want say that it's null. The really big index has the following code for reading in one word at a time:
    Java Code:
    s = new Scanner(new BufferedReader(new FileReader("abcd.txt")));
    
                while (s.hasNext()) {
                    System.out.println(s.next());
                }
    The problem is that this doesn't discriminate when a return has occured since .hasNext treats all white space the same. I want to be able to recognize when there is a return so that I know whether the d part is missing or not. The following is psuedo code for what I am trying to acheive:

    Java Code:
    while (!EOF) {
        a = s.next();
        column.one = a;
        b = s.next();
        column.two = b;
        c = s.next();
        column.three = c;
        d = s.next();
        if (d == 'return') {
            column.four = null;
        } else {
            column.four = d;
        }
    }
    Any ideas?

    Cheers, ScKaSx
    Last edited by ScKaSx; 01-27-2009 at 09:06 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    ScKaSx is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    Yes that is correct. If there is a 'd' then the object gets filled with that value otherwise it gets filled with null.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    hasNext() iterate next element and return true if found. next() returns the next token, if hasNext() return true. So where you stuck with?

    You can get the token using next(). Validate it.

  5. #5
    ScKaSx is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    The problem is:

    Lets say we are on 'c' and the next element is 'a' rather than 'd' (since d is not on that line). In my code I won't know that 'd' isn't there since s.next() looks at the next letter and sees 'a' (remember it doesn't discriminate between space and returns). Therefore, when I load the Column.four it gets loaded with 'a' rather than 'null'.

    Do you see the problem?
    Last edited by ScKaSx; 01-27-2009 at 09:45 AM.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM
  2. about 'return'.
    By helloworld in forum New To Java
    Replies: 9
    Last Post: 11-28-2008, 04:08 AM
  3. Replies: 0
    Last Post: 07-30-2008, 11:25 AM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  5. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 04:20 PM

Posting Permissions

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