posted 11 years ago
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?