Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.nextLine() looping error?

 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys whenever I run this script and go to the help area, it messes up and loops a bunch of times.

Not sure how to fix it but heres the code.

Error is at 40-50 lines into the code.



If your wondering what I'm trying to do, I'm trying to make a basic text based game.
 
Justin Hawtree
Greenhorn
Posts: 3
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 44 needs
typed = input1.nextLine();

The reason is you need to accept a new input or it uses the old one and keeps looping
Edit:
But the main reason why it is looping is because you need to wait for the user input each time you go into the Help method.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jake and Justin, and welcome to the Ranch! Jake, you really need to go through the coding conventions for the Java programming language. Your class name should start with an uppercase letter, all method and variable names with a lowercase letter.

If the recursion in the help() method isn't a requirement of the exercise, it's not necessary. A while or do..while loop would do just as well, and express the intention better.

Also, are you aware that a method can have a return statement that returns program flow to the calling code? and that the return statement can even be inside a code block like an if (...) {...} statement or a while (...) {...) loop?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic