Scanner Requiring Two Inputs
Hello everyone, I’m having some issue with my code. Please keep in mind that I’m very new to programming/java, so understand that simple solutions are a lot easier for me to understand and use. I’m trying to allow the user to make a choice between two options. I’m using a scanner to get the next integer they type, assigning it to an int, and then comparing that to another (pre assigned) int. If they match it should display the first (if) outcome, if not, it should display the second (else) outcome. All this works. The problem is that I have to enter two numbers before it replies; only the first number matters though (eg. I press 1 then enter. Nothing happens, so I press 2 then enter and it tells me I choose option 1.) So if anyone can help me stop it from needing two inputs, it would be greatly appreciated.
Here is my code:
Code :
import java.util.Scanner;
class UserInputsTest {
public static void main(String[] args) {
System.out.println("Type \"1\" for option 1 and \"2\"
for option 2.");
//This prints a line asking for user input
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int checkinput = 1;
scan.nextInt();
//Creates scanner object and assigns the next integer input value
//to int input.
if (input == checkinput) {
System.out.println("You choose option \"1\".");
} else {
System.out.println("You choose option \"2\".");
}
//Checks the input obtained from the scanner against int
//checkinput, if they match it uses the if outcome, if not it uses
//the else outcome.
}
}
Re: Scanner Requiring Two Inputs
Hey, what's up Nate.
There are several great ways to debug your code; one of them, which I particularly advise, is to read through your code line by line and understanding what exactly is being done. Trust me. There's a difference between skimming your code and truly reading it.
Re: Scanner Requiring Two Inputs
Wonder why it would ask for two inputs. Must be someting going on.
Have you read about the Scanner class?
There may be a hint to what is going on in some related paperwork, maybe...
Re: Scanner Requiring Two Inputs
Well, thanks for taking the time to reply. Staticity, trust me, I've read through my code multiple times, translating it into what I think it should be doing as I go, and I don't understand what the problem is. jps, thanks for that link, it kinda helped but most of it seemed unrelated to what I'm trying to accomplish. I guess I still don't understand why it wont print the response until a second thing is entered. Even someone pointing out where the issue is so I can look at it would be very nice.
Re: Scanner Requiring Two Inputs
Quote:
Originally Posted by
Nate
Well, thanks for taking the time to reply. Staticity, trust me, I've read through my code multiple times, translating it into what I think it should be doing as I go, and I don't understand what the problem is.
You should give it another look. Doing it as you go isn't the best idea. You may be repeating your mistakes.
Quote:
Originally Posted by
Nate
jps, thanks for that link, it kinda helped but most of it seemed unrelated to what I'm trying to accomplish.
Here's something I thought was very important from that link. "A scanning operation may block waiting for input."
Just to explain, whenever you ask the scanner to scan something, it will not proceed with any operations until it receives an input value.
Re: Scanner Requiring Two Inputs
Well, I had a long reply written up describing how that really didn't seem like it could be the source of the issue, when one of my arguments made me think, "wait a minute...".
Thanks for the hints guys, I removed the second scan.nextInt and now it works fine. I feel stupid, but at least it's working now.
Re: Scanner Requiring Two Inputs
Haha, that's programming. The most common error is the simplest error. Glad you found it.