Checking JOptionpane for incorrect character inputs

I need help for a java assignment. I'm trying to use the jOptionPane to input a number but I need to check if only numbers were entered. At the bottom of this code at //check if only numbers were entered, you can see what I have so far (It's just an Idea that didn't work out)

Any help is appreciated, I have until 10:50pm (currently 11:23 am) to get this done along with some other stuff related to overloading.


here's the code

public class Lab5Start {
      private static String input;
      public static void main(String[] args) {
            //Enter grade for 4 students through JOptionPane
            int grade1 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 1's Grade"));
            int grade2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 2's Grade"));
            int grade3 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 3's Grade"));
            int grade4 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 4's Grade"));
            
            //create a Jframe_for viewing results and messages
            JFrame frame = new JFrame();
            
            //check if only numbers were entered for grade
            if (Integer.parseInt( "^.*[a-zA-Z].*$")){
                  System.out.println("Please enter numbers only!");
                  System.exit(0);
            }
Grant RichardsStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
There are really two different ways - one of which you are already hinting at.

a. Check the input for all digits (or not all digits) with a regex
b. Catch any exception thrown by Integer.parseInt

In this case (since you have four inputs to collect) i would i would write a method that does that, e.g.

int getInput(String  prompt);

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
You can go the unglamorous route :

public class Lab5Start {
      private static String input;
      public static void main(String[] args) {
            try{
            //Enter grade for 4 students through JOptionPane
            int grade1 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 1's Grade"));
            int grade2 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 2's Grade"));
            int grade3 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 3's Grade"));
            int grade4 = Integer.parseInt(JOptionPane.showInputDialog("Enter Student 4's Grade"));
            
            //create a Jframe_for viewing results and messages
            //JFrame frame = new JFrame();
            
            //check if only numbers were entered for grade 
            //if (Integer.parseInt( "^.*[a-zA-Z].*$")){
            
            
            }catch(Exception e){System.out.println("Please enter numbers only!");}
            }}

Open in new window

0
CEHJCommented:
You should probably arrange a loop until the right input is provided
0
krakatoaCommented:
Answered.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Chat / IM

From novice to tech pro — start learning today.