Reputation: 67
Basically I would like to build a switchcase statement if check if a user input is a integer , float or a String. I would like to use methods such as hasCheckInt() , hasCheckDouble() to check the respective datatype.
Some of the code is missing yes because I do not know what to put in. Forgive the java noob.
import java.util.Scanner;
public class helloWorld {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
switch()) {
case 1:
if(userInput.hasNextInt()) {
System.out.println("Int");
}
break;
case 2:
if(userInput.hasNextLine()) {
System.out.println("String");
}
break;
}
}
}
Upvotes: 1
Views: 66
Reputation: 1074989
Basically I would like to build a switchcase statement if check if a user input is a integer , float or a String. I would like to use methods such as hasCheckInt() , hasCheckDouble() to check the respective datatype.
You can't, the case
s of a switch
must be constants, not the results of an expression (including a function call).
Instead, you'll need if/else if/else
if (hasCheckInt(argumentHere)) {
// `hasCheckInt` returned `true`
// ...
}
else if (hasCheckDouble(argumentHere)) {
// `hasCheckDouble` returned `true`
// ...
}
else {
// None of the above matched
}
Upvotes: 1