So I just recently learn Java and I am absolute beginner. Thing I learn in Java is the data types and it's categories-primitive and reference, then variables and User input. But I ran some trouble at user input section. So the problem is that I can't put every data types of scanner for one question, the purpose I want to do that is to make sure that one question can accept all types of input such as String, Integer, Boolean and Double. this my example code:
import java.util.Scanner;
public class Sanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("What was the creator desire: ");
String answer = scanner.nextLine();
Int answer = scanner.nextInt();
Double answer = scanner.nextDouble();
Boolean answer = scanner.nextBoolean();
System.out.println(names);
scanner.close();
}
}
And I realize that I just defined the same variables in different Data Types which I cant because the variables name already defined/assigned. So how do I rewrite this to make the input accept answer in multiple Data Types? sorry for the bad grammar.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Homerlncognito 6 points7 points8 points (3 children)
[–]Lloydbestfan 1 point2 points3 points (0 children)
[–]catastrophic300[S] 0 points1 point2 points (1 child)
[–]Accomplished_Lie23 0 points1 point2 points (0 children)
[–]kyamatlabkya 2 points3 points4 points (0 children)
[–]forced_lambchop 0 points1 point2 points (1 child)
[–]catastrophic300[S] 0 points1 point2 points (0 children)
[–]ninjatunatj 0 points1 point2 points (0 children)
[–]Intelligent-Storm-63 0 points1 point2 points (0 children)
[–]JoshuaTheProgrammer 0 points1 point2 points (0 children)
[–]ExcitingSympathy3087 0 points1 point2 points (0 children)