Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Getting exceptions in my code... and I have no idea why.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everyone, I'm having a really hard time with this code I have... the object is to determine the average number of characters and average number of letters in a word and average number of words in a sentence (which in this case is just each line in a text document). At the same time, it should be printing all the lines of input in the text document.

I'm fairly certain this code should be working, but clearly it doesn't, giving me exceptions instead of the result I would like to see.



Thanks in advance

EDIT: txtdoc.txt can be anything.. but for an example, I am using

Tell me what thy lordly name
is on the nights plutonian shore
Quoth the raven nevermore
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us the exception stacktrace?
 
Greenhorn
Posts: 16
Android Eclipse IDE C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tried running it (I am just a newbie.... ) that's the error it gave...

Document Contents

Tell me what thy lordly name
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Main.main(Main.java:30)

Using Eclipse (indigo)
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the variable names on line 27 and line 29
 
Ashish L. Sharma
Greenhorn
Posts: 16
Android Eclipse IDE C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, that is the place of error. Do not know why ???
 
Sean Gnau
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashish L. Sharma wrote:Hi, I tried running it (I am just a newbie.... ) that's the error it gave...

Document Contents

Tell me what thy lordly name
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Main.main(Main.java:30)

Using Eclipse (indigo)



^ Exactly the same result I am getting. Sorry for forgetting to post that earlier.
 
Sean Gnau
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Take a look at the variable names on line 27 and line 29



Wow... such a simple mistake... That literally solved everything

@Ashish - It's because I used my original scanner name (reader) which considered the entire document, instead of the scanner name for the single line that I parsed out of the original document.

Thanks a million!
 
Ashish L. Sharma
Greenhorn
Posts: 16
Android Eclipse IDE C++
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the rectified code.... do we have to put hasNext at ---> word = reader.(here)next();??
 
Sean Gnau
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashish L. Sharma wrote:Please post the rectified code....



 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Gnau wrote:Thanks a million!


You're welcome.
Also note that there is no need to initialise your line variable to null on line 19.
will work just as well and you could even do
Same applies to line 28/29
 
reply
    Bookmark Topic Watch Topic
  • New Topic