Answer to Question #297252 in Java | JSP | JSF for isha

Question #297252

write a program that determines whether an input number is an

integer or a real number.


1
Expert's answer
2022-02-13T13:00:40-0500


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();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS