• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieve numerical values from a text file  RSS feed

 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to retrieve numbers from a text file which has text as well as numbers. I have written the following code snippet just to check if I am able to print the numbers in the file.


I get the following exception:

What do I do?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if you are reading an integer with the hasNextInt() method
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Check if you are reading an integer with the hasNextInt() method

Thanks. There is no exception thrown now. But there is no output either
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show the code you modified and the text file contents you intend to read.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note the difference between next(), nextInt() and hasNext(), hasNextInt() methods. If you read the documentation the hasXXX() methods don't advance the scanner to the next position.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:Show the code you modified and the text file contents you intend to read.

Complete code as of now:


Text file (freelast.txt) contents:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shikha Upadhyaya wrote:Text file (freelast.txt) contents:...

Well that should be a clue right there. What is the value of the first token that will be found by Scanner?

Winston
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Well that should be a clue right there. What is the value of the first token that will be found by Scanner?

Winston


A blank space?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shikha Upadhyaya wrote:A blank space?

You could have printed that and seen
Now modify text file like below

Check what happens after 123 is printed
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:
Check what happens after 123 is printed

123 is not printed at all!!! It's the same old blank space.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with the code you posted and got 123 printed... are you sure there is no blank token before that?

Anyways the way you try to print all numbers surrounded by strings in the text file is wrong.

1. Check if scanner has some thing to read using hasNext() in the while condition.

2. Check if the token pointed by the scanner is an integer using hasNextInt() method. Get it using nextInt() and print it. Note that nextInt() moves the scanner to the next position

3. If the condition failed for hasNextInt(), you got to move the scanner to the next position... just call the next() method of the scanner.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:I tried with the code you posted and got 123 printed... are you sure there is no blank token before that?

Yes I am sure. I am using Eclipse Indigo. Well, that shouldn't affect the output in anyway.

John Jai wrote:Anyways the way you try to print all numbers surrounded by strings in the text file is wrong.

1. Check if scanner has some thing to read using hasNext() in the while condition.

2. Check if the token pointed by the scanner is an integer using hasNextInt() method. Get it using nextInt() and print it. Note that nextInt() moves the scanner to the next position

3. If the condition failed for hasNextInt(), you got to move the scanner to the next position... just call the next() method of the scanner.

Thanks, I will try this and post back.
 
Shikha Upadhyaya
Ranch Hand
Posts: 70
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shikha Upadhyaya wrote:
John Jai wrote:I tried with the code you posted and got 123 printed... are you sure there is no blank token before that?

Yes I am sure. I am using Eclipse Indigo. Well, that shouldn't affect the output in anyway.

John Jai wrote:Anyways the way you try to print all numbers surrounded by strings in the text file is wrong.

1. Check if scanner has some thing to read using hasNext() in the while condition.

2. Check if the token pointed by the scanner is an integer using hasNextInt() method. Get it using nextInt() and print it. Note that nextInt() moves the scanner to the next position

3. If the condition failed for hasNextInt(), you got to move the scanner to the next position... just call the next() method of the scanner.

Thanks, I will try this and post back.


Hey it's working!!! Thanks a lot for all your replies. Thank you very much
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!