Im trying to make sure the user is inputing a number and not a word. How would I do this? I tried this, but when I type in numbers it still says that it is a string.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true) {
System.out.println("What is the diameter of the sphere (cm): ");
var diameter = input.next();
if (diameter instanceof String) {
System.out.println("Please enter a number");
} else {
break;
}
}
}
}
nextIntIIRC) or do a conversion after-the-fact, which gives you a bit more flexibility.