StringTokernizer Problem

Hi guy

I am new to java and trying to write a simple program. I am trying to read all the contents from a file and break them in words. I donno why I am just gettin FIRST word when i run the following program.

Basically I just has 1 line in my text file, which is this

(this is the conformation number of the payment which we made => 049071 .)

when i run my program I just see "this" instead of all the words.

WHY?

Can anybody tell me what's wrong i am doin!

Thank alot in advance

try

{

BufferedReader br =new BufferedReader(new FileReader("C:/Documents and Settings/Adeel/Desktop/bell.txt"));

String read = br.readLine();

StringTokenizer stk =new StringTokenizer(read," ");

while(stk.hasMoreTokens())

{

//System.out.println("Test 1");

String result = stk.nextToken();

//System.out.println("Test 2");

//System.out.println("file:" + result);

int i = Integer.parseInt(result);

System.out.println("file starts. "+i);

}

}catch(Exception e)

{

System.out.println(e.getMessage());

}

[1700 byte] By [asyed01a] at [2007-11-27 5:53:19]
# 1

I would guess the problem comes from trying to parse "this" into an int. Could it be that your program complaints about a NumberFormatException or something similar? If so, it's because the call to Integer.parseInt fails and gets you into the catch clause at the bottom, from where you won't get back into the while loop, meaning it never continues to the next word.

HTH

OleVVa at 2007-7-12 15:46:10 > top of Java-index,Java Essentials,New To Java...
# 2

OleVV

Thanks for the quick reply. Actually no i never had any exception i run fine. but not sure why this is happening. Though it works fine if i dont parse and just say something like this.

while(stk.hasMoreTokens())

{

String result = stk.nextToken();

System.out.println(result);

//int i = Integer.parseInt(result);

//System.out.println("file starts. "+i);

}

asyed01a at 2007-7-12 15:46:10 > top of Java-index,Java Essentials,New To Java...
# 3

I'd like to make sure I understand you correctly.

> OleVV

> Thanks for the quick reply. Actually no i never had

> any exception i run fine. but not sure why this is

By "i run fine", do you mean you run without any signs of exceptions or errors, but observe unexpected behaviour?

> happening. Though it works fine if i dont parse and

> just say something like this.

By "it works fine", do you mean that you now get the expected behaviour (all the words)?

Message was edited by:

OleVV

OleVVa at 2007-7-12 15:46:10 > top of Java-index,Java Essentials,New To Java...
# 4
I would have a very hard time believing that you can parse the word "this" with Integer.parseInt() and not get an exception. It may go to somewhere else than your catch block, it may disappear with no trace, I still believe it has happened.
OleVVa at 2007-7-12 15:46:10 > top of Java-index,Java Essentials,New To Java...