This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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

Help with java.util.NoSuchElementException

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am working on code for a class. I am a beginner and need some help. The assignment is 7 parts long and I keep getting an error right away. I'm thinking it is a simple fix, but I can't figure it out.
I've been doing research and am wondering about adding "hasNextLine()", but I don't know where. Thank you for you help.
Here is the error:

Enter a sample text:Exception in thread "main" java.util.NoSuchElementException: No line found
                                                                                                                      at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
                                                                                                                      at AuthoringAssistant.inputString(AuthoringAssistant.java:44)
                                                                                                                      at AuthoringAssistant.main(AuthoringAssistant.java:8)

Here is my code: ( I hope I am doing this correctly, as last time I was told I did not.)
 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, those code tags look correct. Your indentation is however inconsistent; I think there isn't enough indentation before lines 9 and 11, for example.
Where are you suffering the exception? Is it line 44 in what you posted? I can't see how yo ucan get an exception there. Not unless you have closed System.in.
Why have you got several Scanners reading System.in? You only need one.

Please tell us the exact input you are using to get such an exception, including any control characters, e.g. ctrl‑D/ctrl‑Z.
 
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get such error running your code.

One tip: look for the simpler way to implement your current replaceExclamation(String) method. Check String's documentation and see if you could find something useful there. Please report back what you found.
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't get any with Eclipse either, but I'm new to all this Java stuff, so I may not be using Eclipse correctly. I am using 2 Scanners, how can I modify this to use just 1?
 
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make your Scanner a constant using 'final' plus the initializer. Now any member method can use SCNR.

 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply's. This will seem dumb to you guys but if I use the " private static final Scanner SCNR = new Scanner( System.in ); " how do I re-label my scnr's I had previously? (scnr2 and scnr3).
Also will this help me with the String errors I listed at the top of the forum?
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get rid of all other places where you create a new Scanner. Rename all your old Scanner variables  (scnr2 and scnr3) to be the new constant SCNR.

Note: The Java naming conventions say that constant name should be entirely in upper case, hence "SCNR". (I usually name mine "KEYBOARD", but, whatever.)
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may or may not fix your program issues. When you open up more than one Scanner to System.in it becomes unpredictable.
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for you suggestions. I have tried the scanner changes, but the ZyBooks can't seem to get past the "Scanner.nextLine" and String issue(lines 49 & 9, bottom of post under Development errors). I can re-post the code if needed, but the scnr is really all that has changed.

This is the result I get from submitting for grading (we can submit as many times as we need until due date/time). Please notice the errors included as output under the "MENU". It is different errors than the ones I get in development mode. I'll post that below. I hope this helps. I am lost on this.

2: Compare output

Output differs. See highlights below.

Input

We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!


YOUR OUTPUT

Enter a sample text:You entered: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!

Enter your choice : Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at AuthoringAssistant.printMenu(AuthoringAssistant.java:74)
at AuthoringAssistant.main(AuthoringAssistant.java:12)


EXPECTED OUTPUT

Enter a sample text:

You entered: We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!

Choose an option:


Here is the errors in Develop mode:

Enter a sample text:Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at AuthoringAssistant.inputString(AuthoringAssistant.java:49)
    at AuthoringAssistant.main(AuthoringAssistant.java:9)
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my run of your code with SCNR fix:

Enter a sample text:
We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!
You entered:
We'll continue our quest in space.  There will be more shuttle flights and more shuttle crews and,  yes,  more volunteers, more civilians,  more teachers in space.  Nothing ends here;  our hopes and our journeys continue!
MENU
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Enter your choice :
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One fix I would make is to change next() to nextLine() here

 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all your time Carey Brown. I have applied the changes, but obviously I have not done it correctly. Here is my code. I am assuming that the Scanner is not applied correctly. I'm about to give up. I work 60 hrs/week and this is due tomorrow. So no much time left. Thank you anyway for all your time and help.



 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Delete lines 8 & 45 & 64.

Remember, only create ONE Scanner.
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Carey Brown. I deleted the SCNR lines as you said. but I still get the same errors on lines 9 and 49. This maybe something more stringent with ZyBooks than with an IDE. I'm not sure. I do know that all I get is this error in development and receive 9 points out of 20 for the assignment. I got a class @ 1:00pm then work @ 4:00. So I'll try until I go. Thank you for all your help. S. Eckstrom

Enter a sample text:Exception in thread "main" java.util.NoSuchElementException: No line found
   at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
   at AuthoringAssistant.inputString(AuthoringAssistant.java:49)
   at AuthoringAssistant.main(AuthoringAssistant.java:9)
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what have you closed? System.in always implicitly has a next line unless you close something.
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't closed anything that I'm aware of. It appears to be a formatting issue, causing a lack of spacing between lines. Although is highlights the lettering to like I'm missing spacing there too, but I am not.
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On lines 13, 16, & 21, Eclipse is giving me the following: quick fixable warning: The value of the local variable n is not used, I have changed the variable "n" to "i" as that is what is used. I still get the string errors.
 
Ranch Hand
Posts: 60
3
IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is your assignment grading based on test cases?
To me it looks like your Scanner cant find a Line where you are telling him to find one. This may be because you have already reached the end of the input. But this seems unlikely since this is the very first action you are performing.
I think you could avoid the NoSuchElementException by adding


This way your scanner will only go and try to read the line if there actually is a line to read.
This is a bit confusing since I cant tell what your task is. Would you care to share the assignment text?
Dont give up, even if its too late. Do it for the lesson, not for the points!

Greets, a fellow greenhorn!
Louis
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget about what Eclipse says about variables not used. Sort out one problem at a time.
Please confirm that the exception is occurring in the inputString() method. I still think you have closed something. I am afraid that the test with if (scnr.hasNextLine()) ... might not cure your problem. It might simply push the problem elsewhere and you will get different exceptions. Please tell us exactly what input you are giving to produce that exception, including control characters.

Why have you got so many things marked static?

Don't use == true and == false
Both are bad style and error‑prone; if you write = you can get multiple errors for the price of one.
Never if (b == true) ...
Always if (b) ...
Never if (b == false) ...
Always if (!b) ...
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the help and suggestions, unfortunately, I ran out of time. I work full time 60 hrs/week in the evenings. The only time I have to do my homework is on the weekends and a few hours Monday before I leave for work. I could not figure out the problem(s) before the assignment was due Tuesday. I only received credit for 9 out of 20 points on the auto grader, which resulted in a cruddy % points. I have no time to go back over any work, as the next assignments are due weekly and I have 2 other classes as well. Again THANK YOU ALL!, Steve
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S. Eckstrom wrote:. . . THANK YOU ALL! . . .

At least I can say, “That's a pleasure,” about that part of the post. Please consider whether you have bitten off more than you can chew and whether you should reduce your workload by taking longer over your course.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The charAt(0) call seems to be what was causing the issue. If there was a blank line read in from the input somehow, you'll have no characters at all in the string, hence a no such character exception results.
 
S. Eckstrom
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I fix this? I am getting a similar issue with another project.  Thank you for your help.
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there's a possibility that a String might be empty, and therefore you would have a problem trying to find the first character of the String, then before you try to find the first character you should check to see if the String is empty. If it is, then do something else.
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic