Question

In a program I'm making after I let the user input a scanner I create a new Array type "Instructor[]" before having a few if options for the user's integer input. Basically it's:

System...println("Hello, here are your options:..);
Scanner user = new Scanner(System.in);
boolean valid = false;
while(valid==false)
{
   .... make sure it becomes valid...
}
Instructor[] list = new Instructor[2];
if(user.nextInt()==1)
{
   do this
}
else if(==2)
{
  do this
}
... if( ==n)
...
else
{
   System...println("wow");
}

So the 3 options I want appear and if I enter a value outside the while loop works and lets me come back, but then when I enter a valid int the program stops with a blank line, I believe it's because I crate the new Instructor[] array. Thing is I need to make it outside the 3 options because I need to use the same one for each option. I've tried without the while loop and it still is blank, and without the Instructor[] it works, but how can I organize this so that it continues to run when I make a new Instructor[] outside the if statements?

Was it helpful?

Solution

If you only want to prompt the user once per loop, you might want to set user.nextInt() equal to a variable. It looks like the way you are doing it now, the program is asking for new user input for each and every if statement. so try something like this:

if (input==1){
     do this
}
else if (input==2){
     do this
}
else if (input==3){
     do this
}

Is this what you mean?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top