> Java: Multiple User Inputs.?

Java: Multiple User Inputs.?

Posted at: 2014-12-18 
Delete the line:

if(gender > 2){

and its associated closing brace "}"

... and try again. (You don't need it and it doesn't make sense anyway.)

The default statement(s) which you have are correct, so leave as is:

default: System.out.println("Please enter 1 or 2"); // right way to handle other input

Remove the dotted line, or comment it like so:

//-------------------

Good luck!

You can put the whole thing in a while loop and modify the condition variable in the case statments:

import java.util.Scanner;

public class UniqueFunctions {

public static void main(String[] args) {

Scanner uinput = new Scanner(System.in);

int gender;

int boy = 1;

int girl = 2;

boolean play = true;

while(play) {

System.out.println("Are you a boy or a girl? ");

System.out.println("1. Boy |" + " 2. Girl");

gender = uinput.nextInt();

switch(gender){

case 1: gender = 1;

System.out.println("You are a Boy! ");

play = false;

break;

case 2: gender = 2;

System.out.println("You are a Girl! ");

play = false;

break;

default: System.out.println("Please enter 1 or 2");

break;

}

}

}

}

I've been learning Java, and I've encountered a problem with multiple user inputs. I wrote this class to try out using multiple classes:

import java.util.Scanner;

public class UniqueFunctions {



Scanner uinput = new Scanner(System.in);



public void findgender(){

int gender;

int boy = 1;

int girl = 2;





System.out.println("Are you a boy or a girl? ");

System.out.println("1. Boy |" + " 2. Girl");

gender = uinput.nextInt();

if(gender > 2){

switch(gender){

case 1: gender = 1;

System.out.println("You are a Boy! ");

break;



case 2: gender = 2;

System.out.println("You are a Girl! ");

break;



default: System.out.println("Please enter 1 or 2");

---------------

break;

}



}

}

}

It will give me one message and then close, but I want to allow user input once again in the default case where the -------

is. How do I do this?