• Post Reply Bookmark Topic Watch Topic
  • New Topic

hasNext and Files  RSS feed

 
Irving Luna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a program that can read the data from a file text.txt and display No. of words of length < 4, No. of words of length = 4, No. of words of length > 4, Total no. of words read, Total no. of integers read, and the sum of all integers read. I have the following code and I cant seem to figure out why its giving me the wrong input, it appears to skip certain strings and integers.
The file is Called text.txt in the same directory, and it contains the text below without the quotes.
"This 83 is a text file to be read by the next lab assignment. This assignment will count words of difference 46 lengths and report 434 the statistics to the console. The 96 integers interspersed within the text are also read, counted, summed, and reported."
My code:


Reads :
Statistics for file, text.txt:
No. of words of length < 4: 1
No. of words of length = 4: 6
No. of words of length > 4: 8
Total no. of words read : 16
Total no. of integers read: 1
The sum of all integers read: 96

but should display:

Statistics for file, text.txt:
No. of words of length < 4: 16
No. of words of length = 4: 9
No. of words of length > 4: 16
Total no. of words read : 41
Total no. of integers read: 4
Sum of all integers read : 659

I've tried changing the sequence order, and I believe that the problem is with the if/else hasNext() for strings, Can someone explain why its not working?
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Irving Luna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have figure it out.

I was able to compile the code by removing
tempStr = input.next();
from within the if/else statements and placing it below the else if statement, in case anyone had the same issue
from

to

 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example you gave is incorrect. The total no of words should be 45 as you are counting the no of integers.(sumAll = count + countOver + countUnder + countInt;)

And in the if statement, the condition should be >4 and <4. But you have written 3 instead of 4.
And also, in the if statement (in the else of first if statement) you have written input.next() more and more times. Therefore the Scanner will search for another token for each time it read the command, input.next();
So you have to write it only once..

 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What you are doing is using next() twice, so you are missing one word out.
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use try/catch block to handle the bad data (read data from the file).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sveto Koychev wrote:You must use try/catch block to handle the bad data (read data from the file).

Erm...no you mustn't. What Irving is doing is just fine; the problem he's having has just been explained by Campbell.

Winston
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . the problem he's having has just been explained by Campbell.
Actually, I see looking back at the discussion, that Ramesh Pramuditha Rathnayake mentioned that error before I did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!