Check if Input Is Integer

JavaJavaBeginner
Practice Now

Introduction

In Java, there are three methods available to check if the given input is an integer. The first method uses Integer.parseInt() method. The second method uses Scanner.hasNextInt() method. The third method uses Character.isDigit() method. In this lab, we will guide you through all three methods step by step.

Create a Java file

First, open the terminal on Ubuntu system and create a Java file using the following command:

touch ~/project/CheckInputInteger.java

Checking valid integer using Integer.parseInt() method

  1. Create a function named checkValidIntegerUsingIntegerParseIntMethod().
  2. In the function, use the Integer.parseInt() method to parse a string to integer.
  3. Use the try-catch block to handle the exception that is thrown if the input is not a valid integer.
  4. If the parsing is successful, print a message indicating that the given input is a valid integer.
  5. If the parsing is unsuccessful, print a message indicating that the given input is not a valid integer.
  6. Add code comments to the code block.
public static void checkValidIntegerUsingIntegerParseIntMethod() {
        String input = "1234";
        try {
            Integer.parseInt(input);
            System.out.println(input + " is a valid integer");
        } catch (NumberFormatException e) {
            System.out.println(input + " is not a valid integer");
        }
}
  1. Call the function in the main method.
public static void main(String[] args) {
        checkValidIntegerUsingIntegerParseIntMethod();
}

Checking valid integer using Scanner.hasNextInt() method

  1. Create a function named checkValidIntegerUsingScannerHasNextIntMethod().
  2. In the function, use Scanner.hasNextInt() method to check whether the given input is an integer or not.
  3. Print a message indicating that the given input is a valid integer if the input is an integer, otherwise print a message indicating that the given input is not a valid integer.
  4. Add code comments to the code block.
public static void checkValidIntegerUsingScannerHasNextIntMethod() {
        Scanner sc = new Scanner(System.in);
        if(sc.hasNextInt()) {
            System.out.println(sc.nextInt()+" is valid Integer");
        } else {
            System.out.println(sc.nextInt()+" is valid Integer");
        }
        sc.close();
}
  1. Call the function in the main method.
public static void main(String[] args) {
        checkValidIntegerUsingScannerHasNextIntMethod();
}

Checking valid integer using Character.isDigit() method

  1. Create a function named checkValidIntegerUsingCharacterIsDigitMethod().
  2. In the function, use Character.isDigit() method to check each character of the input string to see if it is a digit or not.
  3. If all characters in the string are digits, print a message indicating that the given input is a valid integer.
  4. If non-digit characters exist, print a message indicating that the given input is not a valid integer.
  5. Add code comments to the code block.
public static void checkValidIntegerUsingCharacterIsDigitMethod() {
        String input = "1234";
        Boolean flag=true;
        for(int a=0; a<input.length(); a++) {
            if(a==0 && input.charAt(a) == '-')
                continue;
            if( !Character.isDigit(input.charAt(a)))
                flag=false;
        }
        if(flag) {
            System.out.println(input+" is valid Integer");
        }
   }
  1. Call the function in the main method.
public static void main(String[] args) {
        checkValidIntegerUsingCharacterIsDigitMethod();
}

Summary

In this lab, we learned to use the available methods to check if the given input is an integer or not. We used Integer.parseInt() method, Scanner.hasNextInt() method, and Character.isDigit() method to check if the given input is an integer. We created Java code blocks and added code comments to them. Finally, we ran the code in the terminal of Ubuntu system.

Other Java Tutorials you may like