Results 1 to 4 of 4
- 09-17-2012, 11:48 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
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(); } }
-
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
if a parsing error is encounteredJava Code:if(button == add) {
I would organize things differently. Two possible ways of doing this are (in) pseudocode:
Alternatively, you could doJava 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
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
- 09-17-2012, 11:55 PM #3
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.
- 09-18-2012, 01:32 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Error in reading from a file containing integers
By stud91 in forum Advanced JavaReplies: 1Last Post: 07-07-2012, 05:24 PM -
return method not working?
By Etimer in forum New To JavaReplies: 1Last Post: 03-15-2012, 02:29 AM -
How to display integers in textField
By mDennis10 in forum New To JavaReplies: 11Last Post: 08-12-2011, 02:14 AM -
Error if array contains duplicate integers
By lithium002 in forum New To JavaReplies: 4Last Post: 12-05-2009, 08:58 AM -
return Set .toArray(); method as an array of integers
By maxim in forum New To JavaReplies: 2Last Post: 04-16-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks