In real-world applications, you often need to get input from the user, and this input is typically read as a String
. To perform numerical checks like determining if the input represents an integer, you first need to convert the String
to a numerical type, such as double
.
In this step, we will modify our program to take user input as a String
, convert it to a double
, and then use Math.floor()
to check if the original input represented an integer.
Open the IntegerCheck.java
file in the WebIDE editor.
Replace the existing code with the following:
import java.util.Scanner; // Import the Scanner class
public class IntegerCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create a Scanner object
System.out.print("Enter a number: "); // Prompt the user for input
String input = scanner.nextLine(); // Read user input as a String
try {
// Convert the String input to a double
double number = Double.parseDouble(input);
// Check if the number is an integer using Math.floor()
if (number == Math.floor(number)) {
System.out.println("The input '" + input + "' represents an integer.");
} else {
System.out.println("The input '" + input + "' does not represent an integer.");
}
} catch (NumberFormatException e) {
// Handle cases where the input is not a valid number
System.out.println("Invalid input: '" + input + "' is not a valid number.");
} finally {
scanner.close(); // Close the scanner
}
}
}
Let's look at the new parts of the code:
import java.util.Scanner;
: This line imports the Scanner
class, which is used to read input from the console.
Scanner scanner = new Scanner(System.in);
: This creates a Scanner
object that reads input from the standard input stream (System.in
), which is typically the keyboard.
System.out.print("Enter a number: ");
: This line prompts the user to enter a number.
String input = scanner.nextLine();
: This reads the entire line of input entered by the user as a String
and stores it in the input
variable.
try { ... } catch (NumberFormatException e) { ... }
: This is a try-catch
block. It's used to handle potential errors. In this case, we are trying to convert the String
input to a double
. If the input is not a valid number (e.g., "hello"), a NumberFormatException
will occur, and the code inside the catch
block will be executed.
double number = Double.parseDouble(input);
: This is the crucial part for conversion. Double.parseDouble()
is a static method of the Double
class that attempts to convert a String
into a double
value.
finally { scanner.close(); }
: The finally
block ensures that the scanner.close()
method is called, releasing the system resources used by the Scanner
, regardless of whether an exception occurred or not.
Save the IntegerCheck.java
file.
Now, compile and run the program from the Terminal in the ~/project
directory:
javac IntegerCheck.java
java IntegerCheck
The program will now wait for you to enter input.
Try entering an integer, like 42
, and press Enter. The output should be:
Enter a number: 42
The input '42' represents an integer.
Run the program again and enter a non-integer number, like 3.14
, and press Enter. The output should be:
Enter a number: 3.14
The input '3.14' does not represent an integer.
Run the program one more time and enter something that is not a number, like test
, and press Enter. The output should be:
Enter a number: test
Invalid input: 'test' is not a valid number.
This demonstrates how to handle user input as a String
, convert it to a numerical type, and then apply our Math.floor()
check while also handling potential errors from invalid input.