4 Replies - 2403 Views - Last Post: 21 January 2011 - 12:11 AM Rate Topic: -----

#1 Guest_javajr*


Reputation:

Handling Incorrect Input in a GUI

Posted 20 January 2011 - 02:31 PM

I've got a GUI program written that computes fraction calculations. I haven't written in the code to reduce the fractions yet, but I'm going to. The one thing I need help with is this:

I'm instructing the user to enter 1 for add, 2 for subtract, etc and then using else if statements. But I need the program to pop up an error box if the user enters an incorrect digit for the operand (i.e. anything not between 1-4) or also if they enter a non integer for either of the fractions. I've been trying to write this in but everything I try just causes the program to not do anything. I want an error box to pop up. Can anyone help? Here's my code....

import java.awt.*;
import java.awt.event.*;


import javax.swing.*;


public class FractionCalculator implements ActionListener {


public static void main(String[] args) {
FractionCalculator gui = new FractionCalculator();
}
private JFrame frame;
private JTextField firstNum;
private JTextField secondNum;
private JTextField firstDen;
private JTextField secondDen;
private JTextField operand;
private JLabel answer;
private JButton computeButton;
public FractionCalculator() {
firstNum = new JTextField(2);
firstDen = new JTextField(2);
secondNum = new JTextField(2);
secondDen = new JTextField(2);
operand = new JTextField(2);
answer = new JLabel("Click to Compute");
computeButton = new JButton("Compute");
computeButton.addActionListener(this);
frame = new JFrame("Fraction Calc");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(205,255);
frame.setLayout(new FlowLayout());
frame.add(new JLabel("Enter Numerator & Denominator"));
frame.add(new JLabel("1st Fraction:      "));
frame.add(firstNum);
frame.add(new JLabel("/"));
frame.add(firstDen);
frame.add(new JLabel("2nd Fraction:     "));
frame.add(secondNum);
frame.add(new JLabel("/"));
frame.add(secondDen);
frame.add(new JLabel("Enter Corresponding Number"));
frame.add(new JLabel("1-Add        2-Subtract"));
frame.add(new JLabel("3-Multiply     4-Divide"));
frame.add(new JLabel("Choose Your Operand: "));
frame.add(operand);
frame.add(answer);
frame.add(computeButton);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
String firstNumerator = firstNum.getText();
int fn = Integer.parseInt(firstNumerator);
String firstDenominator = firstDen.getText();
int fd = Integer.parseInt(firstDenominator);
String secondNumerator = secondNum.getText();
int sn = Integer.parseInt(secondNumerator);
String secondDenominator = secondDen.getText();
int sd = Integer.parseInt(secondDenominator);
String oper = operand.getText();
int op = Integer.parseInt(oper);
if (op == 1){
int rfn = fn*sd;
int rfd = fd*sd;
int rsn = sn*fd;
int rn = rfn + rsn;
int rd = rfd;
answer.setText(fn+"/"+fd + "  plus  " + sn+"/"+sd + " = " + rn+"/"+rd);
}
else if (op == 2){
int rfn = fn*sd;
int rfd = fd*sd;
int rsn = sn*fd;
int rn = rfn - rsn;
int rd = rfd;
answer.setText(fn+"/"+fd + " minus " + sn+"/"+sd + " = " + rn+"/"+rd);
}
else if (op == 3){
int rn = fn * sn;
int rd = fd * sd;
answer.setText(fn+"/"+fd + " times " + sn+"/"+sd + " = " + rn+"/"+rd);
}
else if (op == 4){
int rn = fn * sd;
int rd = fd * sn;
answer.setText(fn+"/"+fd + " divided by " + sn+"/"+sd + " = " + rn+"/"+rd);
}
}


}


Is This A Good Question/Topic? 0

Replies To: Handling Incorrect Input in a GUI

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Handling Incorrect Input in a GUI

Posted 20 January 2011 - 03:01 PM

you can use an if statement first:
if(op < 1 || op > 4){
  //pop the error message
}
else {
  if (op == 1){
    ....
}


But if you also want to check non numbers, i'd go with a try\catch block for the Integer.parseInt(oper) code.
Here is a snippet I have written for validating a certain data type:
http://www.dreaminco...snippet5025.htm

you can change it to suit your program.
good luck :)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_javajr*


Reputation:

Re: Handling Incorrect Input in a GUI

Posted 20 January 2011 - 05:56 PM

Thanks Japanir. I've played around with a Try/Catch block, but I can't get the GUI to then calculate. Am I putting it in the wrong place? Should it encompass all of the if/else statements?
Was This Post Helpful? 0

#4 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Handling Incorrect Input in a GUI

Posted 20 January 2011 - 06:16 PM

You can, and you can also set a value to op (something that can't be a valid value for op, like -1).
op will be changed only if the value passed to it is valid.
int op = -1;
try {
  op = Integer.parseInt(oper); 
  }
catch(InputMismatchException ex) {
  ex.printStackTrace();
}

if(op ==....


You can have a look at a simple GUI BMI calculator I wrote. I implement something similar there:
http://www.dreaminco...snippet5031.htm
Was This Post Helpful? 0
  • +
  • -

#5 Guest_javajr*


Reputation:

Re: Handling Incorrect Input in a GUI

Posted 21 January 2011 - 12:11 AM

Okay, I got the error code working correctly - thanks for your help! Turns out I was just placing a bracket in the wrong place. Argh!

Now I'm trying to get my fractions to reduce. I basically have it set up so that it handles a divide by 0 scenario, and displays 1 as the answer if the numerator and denominator are the same, but I cannot get it to reduce the fractions. I tried writing in
else if (rn < rd){
				for (int i = rn; i >= 1; i--){
					while(rd%i == 0){
						int GCD = i;
						rn = rn/GCD;
						rd = rd/GCD;
					}
				} 
				answer.setText("Answer Equals " + rn+"/"+rd);
			}

but then when I hit compute nothing happens.

Here is my full code:
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class FractionCalculator implements ActionListener {

	public static void main(String[] args) {
		FractionCalculator gui = new FractionCalculator();
	}
	
	private JFrame frame;
	private JTextField firstNum;
	private JTextField secondNum;
	private JTextField firstDen;
	private JTextField secondDen;
	private JTextField operand;
	private JLabel answer;
	private JButton computeButton;
	
	public FractionCalculator() {
		firstNum = new JTextField(2);
		firstDen = new JTextField(2);
		secondNum = new JTextField(2);
		secondDen = new JTextField(2);
		operand = new JTextField(2);
		answer = new JLabel("Click to Compute");
		computeButton = new JButton("Compute");
		
		computeButton.addActionListener(this);
		
		frame = new JFrame("Fraction Calc");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(205,255);
		frame.setLayout(new FlowLayout());
		frame.add(new JLabel("Enter Numerator & Denominator"));
		frame.add(new JLabel("1st Fraction:      "));
		frame.add(firstNum);
		frame.add(new JLabel("/"));
		frame.add(firstDen);
		frame.add(new JLabel("2nd Fraction:     "));
		frame.add(secondNum);
		frame.add(new JLabel("/"));
		frame.add(secondDen);
		frame.add(new JLabel("Enter Corresponding Number"));
		frame.add(new JLabel("1-Add        2-Subtract"));
		frame.add(new JLabel("3-Multiply     4-Divide"));
		frame.add(new JLabel("Choose Your Operand: "));
		frame.add(operand);
		frame.add(answer);
		frame.add(computeButton);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event) {
		try {
		String firstNumerator = firstNum.getText();
		int fn = Integer.parseInt(firstNumerator);
		String firstDenominator = firstDen.getText();
		int fd = Integer.parseInt(firstDenominator);
		String secondNumerator = secondNum.getText();
		int sn = Integer.parseInt(secondNumerator);
		String secondDenominator = secondDen.getText();
		int sd = Integer.parseInt(secondDenominator);
		String oper = operand.getText();
		int op = Integer.parseInt(oper);
		
		if (fd == 0 || sd == 0){
			answer.setText("Error: Can't Divide by Zero!");
		}
		else if (op == 1){
			int rfn = fn*sd;
			int rfd = fd*sd;
			int rsn = sn*fd;
			int rn = rfn + rsn;
			int rd = rfd;
			if (rn == 0){
				answer.setText("Answer Equals " + "0");
			}
			else if (rn == rd){
				answer.setText("Answer Equals " + "1");
			}
			else if (rn < rd){
				for (int i = rn; i >= 1; i--){
					while(rd%i == 0){
						int GCD = i;
						rn = rn/GCD;
						rd = rd/GCD;
					}
				} 
				answer.setText("Answer Equals " + rn+"/"+rd);
			}
			else if (rn > rd){
				answer.setText("Answer Equals " + rn+"/"+rd);
			}
		}
		else if (op == 2){
			int rfn = fn*sd;
			int rfd = fd*sd;
			int rsn = sn*fd;
			int rn = rfn - rsn;
			int rd = rfd;
			if (rn == 0){
				answer.setText("Answer Equals " + "0");
			}
			else if (rn == rd){
				answer.setText("Answer Equals " + "1");
			}
			else {
			answer.setText("Answer Equals " + rn+"/"+rd);
			}
		}
		else if (op == 3){
			int rn = fn * sn;
			int rd = fd * sd;
			if (rn == 0){
				answer.setText("Answer Equals " + "0");
			}
			else if (rn == rd){
				answer.setText("Answer Equals " + "1");
			}
			else {
			answer.setText("Answer Equals " + rn+"/"+rd);
			}
		}
		else if (op == 4){
			int rn = fn * sd;
			int rd = fd * sn;
	
			if (rn == 0){
				answer.setText("Answer Equals " + "0");
			}
			else if (rn == rd){
				answer.setText("Answer Equals " + "1");
			}
			else {
			answer.setText("Answer Equals " + rn+"/"+rd);
			}
		}
		else if (op < 1 || op > 4){
			answer.setText("Invalid Operand Entered");
		}
		}
		catch (NumberFormatException f) {
			JOptionPane.showMessageDialog(null,f.getMessage());
		}
	}

}


Was This Post Helpful? 0

Page 1 of 1