Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I would appreciate help with this Java program. Write a static method that check

ID: 3684383 • Letter: I

Question

I would appreciate help with this Java program.

Write a static method that checks if a String represents a valid integer value and returns true if valid, or false if not. For example, "123", "-45", and "0" are valid integers, but "x7" and "3.14" are not. Use java.lang.Integer.parseInt() to perform the actual validity checking inside your method. Your method should also return false if the parameter String is null or empty. It should NOT throw any exceptions back to the caller (but may use exceptions internally). Use the following prototype for your method: public static boolean isValidInt(String testIntval) Show example output for the following values entered: "42" "-123" "12-34" "10.5" "x7" "" (length 0 string) null (pass a Java null reference in a hardcoded call to isValidInt)

Explanation / Answer

public class HelloWorld{

public static boolean isValidInt(String testIntval)
{
    boolean flag=false;
    try
    {
    Integer.parseInt(testIntval);
    System.out.println(testIntval+" is a number");
    }
    catch(NumberFormatException e)
    {
        System.out.println(testIntval +": Not a number");
    }

    return flag;
}
     public static void main(String []args){
        System.out.println("Hello World");
        HelloWorld.isValidInt("42");
        HelloWorld.isValidInt("-123");
        HelloWorld.isValidInt("12-34");
        HelloWorld.isValidInt("10.5");
        HelloWorld.isValidInt("x7");
        HelloWorld.isValidInt("");
     }
}