Can we ask a question to the user and take input from the answer we got in JAVA? If possible, how? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we ask a question to the user and take input from the answer we got in JAVA? If possible, how?

29th Jan 2017, 11:25 AM
tharun surya dwivedula
tharun surya dwivedula - avatar
2 Answers
+ 2
use a scanner. ----------------- System.out.println("What is your name?"); Scanner input = new Scanner(System.in); String name = input.nextLine(); System.out.println("Your name is " + name);
30th Jan 2017, 12:37 AM
LordHill
LordHill - avatar
+ 1
If you intend to have alot of input, you can simplify it. make it a method like this and build your question right into it... FOR STRING INPUT - public void String inputString(String A){ System.out.println(A); Scanner input = new Scanner(System.in); String in = input.nextLine(); return in; } FOR INTEGER INPUT - public void int inputInt(String A){ System.out.println(A); Scanner input = new Scanner(System.in); int in = input.nextInt(); return in; } Now use the methods with built in questions - String job = inputString("What do you do?"); String name = inputString("What is your name?"); int age = inputInt("How old are you?"); int weight = inputInt("How Much do you weigh?");
30th Jan 2017, 1:01 AM
LordHill
LordHill - avatar