Question:
Java----I use my scanner object twice, but it only asks for input once?
kid
2011-11-19 18:55:37 UTC
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
//some code later. . . . .
String nextinput = input.next();

And when i run my code, it asks for input on the first variable 'choice'. However, then it runs, gets to 'nextinput' and doesn't ask for input again--instead, the program just assumes that the input i already gave it is the same for the second object. Is this a problem with my code(I will figure it out if this is the case) or is it a problem with the way I am using Scanner objects?
Three answers:
b4iquit
2011-11-20 01:37:34 UTC
The following code works.

note changes to input you declare input both inside and outside of main, so the one initialised inside main is not available to the rest of the class, the one outside main stays unintialised

so replace

Scanner input = new Scanner(System.in);

with

input = new Scanner(System.in);

You were also setting input to null just before the second data input( the 1 or 2 choice). Just delete that line it is not wanted.



Be safe, be sage





import java.util.Scanner;



public class GameInterface {



static Scanner input;



public static void main(String args[]){

input = new Scanner(System.in);



GameInterface.printIntroMessage();

int choice = input.nextInt();



if(GameInterface.CheckIsValid(choice, 3)){

if( GameInterface.AskforConfirm(choice) ) {

System.out.println("Destination Has Been Reached!!!!!");

}

}



}//end method



//Prints a the game's first message to the user.

private static void printIntroMessage(){

System.out.println("Welcome to Aiur, what would you like to do?");

System.out.println("1. Explore Aiur.");

System.out.println("2. Recall a personality.");

System.out.println("3. Live in Aiur.");



}//end method



//This method checks to see if the given choice is actually allowed (a

//choice of 5 when there are 3 choices would not be allowed, for example)

//TRUE means the choice is valid, FALSE means the choice is invalid

private static boolean CheckIsValid(int SelectedChoice, int NumOfChoices){

int choice = SelectedChoice;

if(choice > 0 && choice < NumOfChoices + 1){

return true;

}else{

System.out.println("You have selected an invalid choice, choose again.");

return false;

}//end if/else

}//end method



//This method is intended to be used when risky or important decisions are made.

//It asks the user if they wish to go through with their decision.

//TRUE means the choice is the one wanted, FALSE means the choice is not the one wanted

private static boolean AskforConfirm(int SelectedChoice){

int choice = SelectedChoice;



System.out.println("You have chosen: " + choice);

System.out.println("Is this your final choice?");

System.out.println("1. yes");

System.out.println("2. no");



String nextinput = input.next();

int confirm = Integer.parseInt(nextinput);



//DID THE USER GIVE A 1 OR 2 IN RESPONSE?



if( GameInterface.CheckIsValid(confirm,2) ) { //the user did give a good response

if(confirm == 1){

return true;

}else{

return false;

}

}else{ //the user did NOT give a good response

System.out.println("You must supply a '1' or '2' to answer this question.");

System.out.println("I will ask again:");

return GameInterface.AskforConfirm(choice);

}

}



}//end class
modulo_function
2011-11-20 03:26:10 UTC
How can we diagnose problems with your code without seeing it?



Post your code for help with your code.



+add

Why are you setting input to null in your method AskforConfirm(..)? Since input is your scanner you're 'disconnecting' your scanner. I'm starting my NetBeans and will further diagnose and report.



++add

Does this code even compile? Look at the braces matching around main(..) and you'll see that the closing bracket that you have labeled as end method actually ends the class! Did you fiddle with the code before copying and pasting?

+++add

I see, the YA window omitted some code and there's probably an opening { at the end of the ... code. When posting code to the YA window you need to break long lines into shorter ones with lots of line breaks.

++++add

I can't make enough assumptions to get code that I can diagnose. If you really want to you can send me the source file. Just send a message and I reply with my real email I do know that sometimes next() and nextInt() have problems when mixed with other reads and writes



+final

I had this user send me his code See that first statement inside main(..)? It redefined the identifier input to be a scanner local to main and thus the class lever scanner input is not defined.
?
2011-11-20 03:02:15 UTC
It could be a problem with your choice of naming for the second object. Instead of naming it nextinput (which could be causing a syntax error in the program) name it something else.



More likely, though, it is a syntax or semantic error in the surrounding code that is preventing it from running that particular line.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...