• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 64
  • Last Modified:

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);
            }
0
Grant Richards
Asked:
Grant Richards
  • 2
  • 2
3 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now