Results 1 to 4 of 4
  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default RegEx not being recognized

    I have a program in which I am prompting users for integer values to display in a JFrame. I call the method below to load an array with their input:

    Java Code:
    	public String inputAssembly(){
    		
    		if (!jtfInput.getText().matches("\\d")){
    			JOptionPane.showMessageDialog(null, "Input must be of integer value.");
    		}
    		
    		if (jrbFar.isSelected()){
    			return jtfInput.getText() + jrbFar.getText();
    		}
    		else{
    			return jtfInput.getText() + jrbCel.getText();
    		}
    		
    	} // End of inputAssembly
    This method is being called from the controller via an ActionListener control ...

    Java Code:
    	// CONTINUE BUTTON CLICK ACTIONS
    	class ContinueButtonListener implements ActionListener{
    		
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    
    			input = view.inputAssembly();
    				
    			myArray[btnClick] = input;
    			JOptionPane.showMessageDialog(view, "Got it!");
    					
    			btnClick++;
    			view.clearTextbox();
    			
    		} // End of actions
    Regardless of the input, both messages display (invalid input / got it). I've tried debugging so I know that the values are getting entered and read correct, at least to my knowledge.

    Anyone notice something wrong? It is a very simple regular expression, only checking to be sure an integer was entered.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: RegEx not being recognized

    Well, the matches statement matches the entire string. But you are only matching against a single digit. Two or more digits would fail. If you want to match on one or more digits use \\d+

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: RegEx not being recognized

    Thanks, Jim. That worked perfectly for accepting integer values. It still displays both messages if you enter a letter and inserts it into the array. Shouldn't that regular expression only accept integers?

    EDIT: After additional thought it is operating correctly, I am just handling my events wrong. I have to edit something so that doesn't get sent back and inserted into the array. I'll think on this for a bit. Thanks again for your help.
    Last edited by mallorz; 01-27-2014 at 03:06 AM.

  4. #4
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    65
    Rep Power
    0

    Default Re: RegEx not being recognized

    Got it ..

    Java Code:
    	// METHOD TO RETRIEVE TEXTBOX INPUT
    	public String inputAssembly(){
    		
    		if (!jtfInput.getText().matches("\\d+")){
    			JOptionPane.showMessageDialog(null, "Input must be of integer value.");
    		}
    		
    		else if (jrbFar.isSelected()){
    			return jtfInput.getText() + jrbFar.getText();
    		}
    		else{
    			return jtfInput.getText() + jrbCel.getText();
    		}
    		return null;
    		
    	} // End of inputAssembly
    Java Code:
    	// CONTINUE BUTTON CLICK ACTIONS
    	class ContinueButtonListener implements ActionListener{
    		
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// TODO Auto-generated method stub
    
    			input = view.inputAssembly();
    				
    			if (input !=null){
    			myArray[btnClick] = input;
    			JOptionPane.showMessageDialog(view, "Got it!");
    					
    			btnClick++;
    			view.clearTextbox();
    			}
    			else 
    				view.clearTextbox();
    			
    		} // End of actions

Similar Threads

  1. javac not recognized
    By kris972 in forum New To Java
    Replies: 7
    Last Post: 09-21-2012, 07:11 AM
  2. javac is not recognized
    By double_007 in forum New To Java
    Replies: 10
    Last Post: 09-07-2012, 02:26 AM
  3. 'javac' is not recognized
    By adm in forum New To Java
    Replies: 2
    Last Post: 01-31-2012, 09:24 AM
  4. Replies: 4
    Last Post: 01-27-2012, 06:27 AM
  5. .Jar Not Recognized
    By Java Tyler in forum New To Java
    Replies: 11
    Last Post: 08-12-2010, 03:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •