• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

hasNext and Files

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Irving Luna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

 
Marshal
Posts: 80133
418
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What you are doing is using next() twice, so you are missing one word out.
 
Greenhorn
Posts: 29
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must use try/catch block to handle the bad data (read data from the file).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80133
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic