write a program that determines whether an input number is an
integer or a real number.
import java.util.Scanner;
class App {
public static void main(String[] args) {
java.util.Locale.setDefault(new java.util.Locale("en-US", "en-US"));
Scanner keyBoard = new Scanner(System.in);
System.out.print("Ente a number: ");
String numberStr = keyBoard.nextLine();
if (numberStr.contains(".")) {
try {
Double.parseDouble(numberStr);
System.out.println("The number is real");
} catch (Exception e) {
System.out.println("Wrong value");
}
} else {
try {
Integer.parseInt(numberStr);
System.out.println("The number is integer");
} catch (Exception e) {
System.out.println("Wrong value");
}
}
keyBoard.close();
}
}
Comments
Leave a comment