Results 1 to 6 of 6
- 01-27-2009, 08:31 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
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:
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:s = new Scanner(new BufferedReader(new FileReader("abcd.txt"))); while (s.hasNext()) { System.out.println(s.next()); }
Any ideas?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; } }
Cheers, ScKaSxLast edited by ScKaSx; 01-27-2009 at 09:06 AM.
- 01-27-2009, 08:55 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you want to check that at the end there is 'd' found or not?
- 01-27-2009, 09:06 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
Yes that is correct. If there is a 'd' then the object gets filled with that value otherwise it gets filled with null.
- 01-27-2009, 09:14 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-27-2009, 09:42 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
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.
- 01-27-2009, 10:11 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Then how about the reading line by line in the text file? On a single line process, using a regular expression may be.
Similar Threads
-
Using int/int, 7/5 would return 1
By zoe in forum New To JavaReplies: 2Last Post: 12-02-2008, 11:25 AM -
about 'return'.
By helloworld in forum New To JavaReplies: 9Last Post: 11-28-2008, 04:08 AM -
SOS!!HELP!!Code for recognize a printer installed by the name...
By Anna in forum New To JavaReplies: 0Last Post: 07-30-2008, 11:25 AM -
need help with scanner
By whiterex in forum New To JavaReplies: 1Last Post: 04-22-2008, 01:41 PM -
if..else..return
By mqdias in forum New To JavaReplies: 1Last Post: 08-10-2007, 04:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks