This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes .nextLine() looping error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ".nextLine() looping error?" Watch ".nextLine() looping error?" New topic

.nextLine() looping error?

Jake Cirino

Joined: Jul 27, 2012
Posts: 17

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

Joined: Jul 27, 2012
Posts: 3

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
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

Joined: May 03, 2008
Posts: 5045

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?

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: .nextLine() looping error?
It's not a secret anymore!