Results 1 to 4 of 4
  1. #1
    jeduhi is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default return error if TextField contains non-integers - NOT WORKING!

    I am trying to validate that the characters entered in the two TextField's are ints. Instead my code is treating anything that is not an integer like a "0"

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class simpleCalculator {
    	
    	private JFrame frame;
    	private JButton add, subtract, multiply, divide;
    	private JLabel result;	
    	private JTextField text1, text2;	
    	private int b1, b2;
    	
    	public simpleCalculator() {
    		frame = new JFrame();
    		frame.setLocation(100,100);
    		frame.setSize(400,400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("My Simple Calculator");
    		
    		initializeComponents();
    		
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	public void initializeComponents() {
    		//method to create panels. Three panels are needed for simpleCalculator
    		JPanel textPanel = new JPanel();
    		JPanel resultPanel = new JPanel();
    		JPanel buttonPanel = new JPanel();
    		
    		//method to create and add text boxes to textPanel.
    		text1 = new JTextField(10);
    		text2 = new JTextField(10);
    		textPanel.add(text1);
    		textPanel.add(text2);
    				
    		//method to add result to resultPanel.
    		result = new JLabel("Result: ");
    		resultPanel.add(result);
    		
    		//method to create buttons.
    		add = new JButton("ADD");
    		subtract = new JButton("SUB");
    		multiply = new JButton("MULT");
    		divide = new JButton("DIV");
    		
    		//method to add buttons.
    		buttonPanel.add(add);
    		buttonPanel.add(subtract);
    		buttonPanel.add(multiply);
    		buttonPanel.add(divide);
    		
    		//initialize calculate
    		calculate c = new calculate();
    		
    		//add ActionListener to buttons
    		add.addActionListener(c);
    		subtract.addActionListener(c);
    		multiply.addActionListener(c);
    		divide.addActionListener(c);		
    		
    		//method to add panels to frame.
    		frame.add(textPanel, BorderLayout.PAGE_START);
    		frame.add(resultPanel, BorderLayout.LINE_START);
    		frame.add(buttonPanel, BorderLayout.PAGE_END);		
    	}
    	
    	class calculate implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			Object button = e.getSource();
    			try {
    				b1 = Integer.parseInt(text1.getText());
    			}
    			catch(NumberFormatException nfe) {
    				result.setText("Result: ERROR");
    			}
    			try {
    				b2 = Integer.parseInt(text2.getText());
    			}
    			catch(NumberFormatException nfe) {
    				result.setText("Result: ERROR");
    			}
    			if(button == add) {
    				int total = b1 + b2;
    				String r = Integer.toString(total);
    				result.setText("Result: " + r);
    			}
    			if(button == subtract) {
    				int total = b1 - b2;
    				String r = Integer.toString(total);
    				result.setText("Result: " + r);
    			}
    			if(button == multiply) {
    				int total = b1 * b2;
    				String r = Integer.toString(total);
    				result.setText("Result: " + r);
    			}
    			if(button == divide) {
    				if(b2 == 0) {
    					result.setText("Result: ERROR");
    				}
    				else {
    					int total = b1 / b2;
    					String r = Integer.toString(total);
    					result.setText("Result: " + r);
    				}
    			}
    		}
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new simpleCalculator();
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: return error if TextField contains non-integers - NOT WORKING!

    You are doing a great job of catching for number format errors, but then your method keeps right on rolling along if an error is encountered. In other words, there's nothing stopping the code beginning with

    Java Code:
    if(button == add) {
    if a parsing error is encountered

    I would organize things differently. Two possible ways of doing this are (in) pseudocode:

    Java Code:
    try 
      parse int for text1
      parse int for text2
    catch
      notify user of error
      return out of method ***** you're not doing this
    
    
    // outside of try/catch block, do rest of method's processing here
    Alternatively, you could do

    Java Code:
    try 
      parse int for text1
      parse int for text2
    
      // do everything else that needs to be done in the method here while still in the try block
    catch
      notify user of error

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: return error if TextField contains non-integers - NOT WORKING!

    Add a boolean flag to that is set to indicate bad values in the text fields
    Or exit the listener method when bad input is detected. Don't ignore the bad input and continue.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jeduhi is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: return error if TextField contains non-integers - NOT WORKING!

    Quote Originally Posted by Fubarable View Post
    You are doing a great job of catching for number format errors, but then your method keeps right on rolling along if an error is encountered. In other words, there's nothing stopping the code beginning with

    Java Code:
    if(button == add) {
    if a parsing error is encountered

    I would organize things differently. Two possible ways of doing this are (in) pseudocode:

    Java Code:
    try 
      parse int for text1
      parse int for text2
    catch
      notify user of error
      return out of method ***** you're not doing this
    
    
    // outside of try/catch block, do rest of method's processing here
    Alternatively, you could do

    Java Code:
    try 
      parse int for text1
      parse int for text2
    
      // do everything else that needs to be done in the method here while still in the try block
    catch
      notify user of error
    Thank you so much! I was not returning out of the catch. Once I put a return in everything works as it should as far as I can tell.

Similar Threads

  1. Error in reading from a file containing integers
    By stud91 in forum Advanced Java
    Replies: 1
    Last Post: 07-07-2012, 06:24 PM
  2. return method not working?
    By Etimer in forum New To Java
    Replies: 1
    Last Post: 03-15-2012, 03:29 AM
  3. How to display integers in textField
    By mDennis10 in forum New To Java
    Replies: 11
    Last Post: 08-12-2011, 03:14 AM
  4. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 09:58 AM
  5. Replies: 2
    Last Post: 04-16-2008, 01:35 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
  •