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:
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:
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