• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!