• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Word count problem.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble making a java program that reads a document and tells the user how many words there are in the document however it keeps giving me the following error:



this is what i got so far.



Any help appreciated. Thanks.
 
Marshal
Posts: 78659
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

How are you iterating through the text? You appear to use input.hasNext() as a test in your while loop. You are never doing anything with input in that loop, so that will always return true, until your i variable falls off the end of the String and you suffer a StringIndexOutOfBoundsException.
You will have to use the length of the String to tell whether you need to continue the loop.
 
Avi Chat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the prompt reply .



Would it be something like that? but i am still getting errors. Still very new at this :S
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Avi Chat wrote:Would it be something like that? but i am still getting errors. Still very new at this :S



This is a common newbie mistake. You take too big a bite and then you end up with a heap of non-working code. Instead always develop programs in small incremental steps. Make sure everything works as expected before moving on. In this way your program will be working from start to finish. This is called Stepwise Refinement and it is the mother of all program development methods.

So start with simple program that just reads a a text file line by line and prints the lines to the screen. Then add functionality litle by little in small working increments.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic