try{
           name= JOptionPane.showInputDialog("Enter Employee Name: ");
            }
            <br />
        catch (Exception e){
         //wHAT IS THE CODE HERE if  THE INPUT IS NUMERIC
         JOptionPane.showInputDialog("Invalid Input");
       }

Recommended Answers

Well here is one solution:

public class IsItAnInt {
    public static boolean isInt(String number){
        try{
            int a = Integer.parseInt(number);
            return true;
        }
        catch(Exception evt){
            return false;
        }
    }

    public static void main(String[] args){
        System.out.println("is 12 an int? : " + isInt("12"));
        System.out.println("is 1234fd an int?: " + …
Jump to Post

I just gave you the method, just use isInt() method. Take out the try and catch and use an if/else statement.

Jump to Post

All 5 Replies

Well here is one solution:

public class IsItAnInt {
    public static boolean isInt(String number){
        try{
            int a = Integer.parseInt(number);
            return true;
        }
        catch(Exception evt){
            return false;
        }
    }

    public static void main(String[] args){
        System.out.println("is 12 an int? : " + isInt("12"));
        System.out.println("is 1234fd an int?: " + isInt("1234fd"));
    }
}

You can use the isInt method to find out whether it is in int or not. Also take out the <br />, this is Java not HTML.

 try{
           name= JOptionPane.showInputDialog("Enter Employee Name: ");
            }
        catch (Exception e){
         //wHAT IS THE CODE HERE if  THE INPUT IS NUMERIC instead of STRING since name is string...
         JOptionPane.showInputDialog("Invalid Input");
       }

I just gave you the method, just use isInt() method. Take out the try and catch and use an if/else statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.