• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Loop Help  RSS feed

 
Jorren Lenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still relatively new to java and am working on a lab program. I have already met the requirements and am trying to spice my code up with a loop that prompts the user to say if the program should run again. This is what I have so far:



However, it is giving me an error after the user inputs "y" in for the answer to run again. What did I do wrong?

Error code is:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at lab2.OrderedPairTest.main(OrderedPairTest.java:11)
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see you are avoiding the problems people have with Scanner#nextLine by not using anything else. But then you are closing the Scanner. That is something you mustn't do. Yes, I know evrybody tells you to close things, but that only applies to files or sockets. It doesn't apply to System.in. When you close the Scanner, you are also closing System.in, which will no longer work and can never be reopened.

You don't need two Scanners pointing to System.in. Use one and keep it open.

Some style things. Never use == false or == true or similar. It is not while (b == true)… but while (b)… The == sign is not necssary at all. And it is error‑prone because it is easy to write = instead. If you want false, use !b rather than b.
Don't use if-else to set a boolean. Simply retry = nextInput.equals("yes"); Clearer and you have six lines less code to go wrong.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you know you can have a switch with Strings? It was introduced in Java7.
 
Jorren Lenson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the warm welcome! . I get what you are saying now about he Scanners being closed. I only closed them off because eclipse gave me a warning that there was a resource leak because they were still opened. But as soon as I reopened them the app. worked like a charm. Thanks!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to take Eclipse warnings with a pinch of salt. Remember they are suggestions not instructions and you can ignore them.

It would be a resource leak for everything else, except System.in. You still shouldn't close a Scanner/Reader/Whatever pointing to anything else. you should use try‑with‑resources and let the JVM call the close() method for you. Try‑with‑resources is not available in Java6 or earlier.

As I said you can writeThere is a lot of repeated code in your app. Look at lines 16 21 27 34 43. There must be a way only to call line 16 once.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And … “You're welcome”
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!