Problems using Scanner

Hi,

I'm having problems with a small piece I've code I've written, which implements Scanner, not behaving as predicted.

What I would like it to do is read in a line of text in an expected format, print the line then print each number in the line on a new line followed by the string, before moving on to the next line in the txt file. For example if the input is:

1 0 5 6 "Frame",

2 0 5 6 "Exposure",

...

I would like the code to out put:

1 0 5 6 "Frame",

1

0

5

6

"Frame"

2 0 5 6 "Exposure",

2

0

5

6

"Exposure"

etc.

Instead what I get is:

1 0 5 6 "Frame",

2 0 5 6 "Exposure",

...

1

0

5

6

"Frame"

And that's it. The code only breaks up the lines after its read them all in and even then it only processes the first line. Here's the code:

import java.io.*;

import java.util.Scanner;

publicclass scanText{

publicstaticvoid main(String[] args)throws IOException{

Scanner fs =null;

Scanner ts =null;

String token =null;

String msg =null;

int num;

try{

fs =new Scanner(new BufferedReader(new FileReader("testText.txt")));

fs.useDelimiter("\\r");

while (fs.hasNext()){

token = fs.next();

System.out.println(token);

ts =new Scanner(token);

ts.useDelimiter("\\s*");

while (ts.hasNextInt()){

num = ts.nextInt();

System.out.println(num);

}

if (ts.hasNext()){

ts.useDelimiter(",");

msg = ts.next();

System.out.println(msg);

}

}

}finally{

if (fs !=null){

fs.close();

}

if (ts !=null){

ts.close();

}

}

}

}

I'm sure I'm missing something simple in my implementation. Could a fresh eye please help me out?

Thanks

[3368 byte] By [msparka] at [2007-11-27 10:37:18]
# 1

use a system.out.println(); in your while loops

schumachera at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 2

Keep it in one thread please!

http://forum.java.sun.com/thread.jspa?threadID=5195036

prometheuza at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 3

> use a system.out.println(); in your while loops

Er, all that does is give me the same out put as before, but with new lines between the last tokens in the last broken up line:

1 0 5 6 "Frame",

2 0 5 6 "Exposure",

3 5 3 3 "Filter A or Filter B",

4 6 2 2 "Transfer",

1

0

5

6

"Frame"

I still don't understand why the last while loop is being processed after the first one and even then only the first line is processed.

msparka at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 4

Keep it in one thread please!

http://forum.java.sun.com/thread.jspa?threadID=5195036

prometheuzza at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 5

> Keep it in one thread please!

> http://forum.java.sun.com/thread.jspa?threadID=5195036

Sorry? I don't understand, is this posted on more than one forum?

msparka at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 6

> Sorry? I don't understand, is this posted on more

> than one forum?

Perhaps you clicked twice while submitting your original post, but as you can see you posted here as well:

http://forum.java.sun.com/thread.jspa?threadID=5195036

Anyway, I suggest continuing there.

prometheuzza at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 7

> > Sorry? I don't understand, is this posted on more

> > than one forum?

>

> Perhaps you clicked twice while submitting your

> original post, but as you can see you posted here as

> well:

> http://forum.java.sun.com/thread.jspa?threadID=5195036

>

>

> Anyway, I suggest continuing there.

oops, sorry, thanks for pointing that out!

How do I delete this thread?

msparka at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...
# 8

> oops, sorry, thanks for pointing that out!

No problem.

> How do I delete this thread?

No, you can't delete (your own) threads. Just let this one be and if you have further questions, you can post them in the other thread.

Thanks.

prometheuzza at 2007-7-28 18:47:10 > top of Java-index,Java Essentials,New To Java...