Videos
Hi - Im learning Java and doing a course in my UNI where I am asked to create a program.
The exercise is to create a program that handles user input and converts it to the correct format. The user indicates the type of input that he/she will be providing and then follows that up by stating the actual input.
The types of input are: int / double / string
The program first asks the user to indicate the type of input
This is my code so far:
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
// Write your code here
Scanner scanner = new Scanner(System.in);
// Scanner for user input
System.out.println("Please input the type of input you would like to print:");
String message = scanner.nextLine();
int intValue = Integer.parseInt(scanner.nextLine());
double doubleNumber = Double.parseDouble(scanner.nextLine());
System.out.println("Please give the input:");
}
}
This is wrong and I know that, but I'm struggling to figure out how to create a program that scans the user input and decides if it's a string/int or double. I tried using a converter as I figured that the user input would be a string as it's indicating by writing "int" or "double" or "string" hoping that my program would pick up on the next line and decide if it was an int/string or double. But as I realized myself, using .nextLine() on all three of them makes this complicated.
Any suggestions?