Hi,
I am using actionlistener so that when a button is pressed, a calculation is made from an entered string (which gets converted to a double) and the result is displayed in a JTextField.
The program is a calculater for currency (pound to euros OR euros to pounds) and there are two currency fields and the result field, as well as a button (that performs the calulation when clicked).
I am using two try and catches and am making an applet. The program runs fine however the catches will always pop up in the console for whichever field is left empty. The only way the catches do not pop up is if both field are filled, but then the program will not perform properly.
Any suggestions on what I can do? Thanks
ActionListener problem, try/catch issue
Page 1 of 114 Replies - 1247 Views - Last Post: 12 May 2011 - 10:11 AM
Replies To: ActionListener problem, try/catch issue
#2
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:10 AM
Please post code
#3
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:14 AM
myButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try{
myStirlingString = myStirlingField.getText();
stirConv = Double.parseDouble(myStirlingString);
poundToEuro = stirConv * rateExchange;
NumberFormat formatter = new DecimalFormat("#0.00");
double newfor = Double.parseDouble(formatter.format(poundToEuro));
myCurrencyConField.setText("€"+ Double.toString(newfor));
}
catch(NumberFormatException d)
{
System.out.println("You messed up son!!!");
}
try{
myEuroString = myEuroField.getText();
euroConv = Double.parseDouble(myEuroString);
euroToPound = euroConv / rateExchange;
NumberFormat formatter = new DecimalFormat("#0.00");
double secfor = Double.parseDouble(formatter.format(euroToPound));
myCurrencyConField.setText("£"+ Double.toString(secfor));
}
catch(NumberFormatException d)
{
System.out.println("You shafted up son!!!");
}
}
{
public void actionPerformed(ActionEvent e)
{
try{
myStirlingString = myStirlingField.getText();
stirConv = Double.parseDouble(myStirlingString);
poundToEuro = stirConv * rateExchange;
NumberFormat formatter = new DecimalFormat("#0.00");
double newfor = Double.parseDouble(formatter.format(poundToEuro));
myCurrencyConField.setText("€"+ Double.toString(newfor));
}
catch(NumberFormatException d)
{
System.out.println("You messed up son!!!");
}
try{
myEuroString = myEuroField.getText();
euroConv = Double.parseDouble(myEuroString);
euroToPound = euroConv / rateExchange;
NumberFormat formatter = new DecimalFormat("#0.00");
double secfor = Double.parseDouble(formatter.format(euroToPound));
myCurrencyConField.setText("£"+ Double.toString(secfor));
}
catch(NumberFormatException d)
{
System.out.println("You shafted up son!!!");
}
}
#4
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:21 AM
Don't see ANY reason to use a try/catch in an ActionListener while performing math
You will have to show up your code
unless it is to catch NumberFormatException from the JTextField
You will have to show up your code
unless it is to catch NumberFormatException from the JTextField
#5
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:28 AM
ok, but if remove the try catches and just leave the rest of the code I get a load of errors in console when it runs even though the applet works. eg
myButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
myStirlingString = myStirlingField.getText();
stirConv = Double.parseDouble(myStirlingString);
poundToEuro = stirConv * rateExchange;
NumberFormat formatter = new DecimalFormat("#0.00");
double newfor = Double.parseDouble(formatter.format(poundToEuro));
myCurrencyConField.setText("€"+ Double.toString(newfor));
myEuroString = myEuroField.getText();
euroConv = Double.parseDouble(myEuroString);
euroToPound = euroConv / rateExchange;
NumberFormat formattertwo = new DecimalFormat("#0.00");
double secfor = Double.parseDouble(formattertwo.format(euroToPound));
myCurrencyConField.setText("£"+ Double.toString(secfor));
}
also, to be honest I thought it would be a good idea for an error to show in the console if both fields were left blank, something saying, please enter amount. (instead of "you shafted up" bit).
myButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
myStirlingString = myStirlingField.getText();
stirConv = Double.parseDouble(myStirlingString);
poundToEuro = stirConv * rateExchange;
NumberFormat formatter = new DecimalFormat("#0.00");
double newfor = Double.parseDouble(formatter.format(poundToEuro));
myCurrencyConField.setText("€"+ Double.toString(newfor));
myEuroString = myEuroField.getText();
euroConv = Double.parseDouble(myEuroString);
euroToPound = euroConv / rateExchange;
NumberFormat formattertwo = new DecimalFormat("#0.00");
double secfor = Double.parseDouble(formattertwo.format(euroToPound));
myCurrencyConField.setText("£"+ Double.toString(secfor));
}
also, to be honest I thought it would be a good idea for an error to show in the console if both fields were left blank, something saying, please enter amount. (instead of "you shafted up" bit).
#6
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:38 AM
Keep your try/catch for the part of your code that Double.parseDouble(JTextField.getText())
in the catch claude simply return
If the user is too dumb to enter an invalid number no need to process further
in the catch claude simply return
If the user is too dumb to enter an invalid number no need to process further
#7
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:52 AM
You're actually making things fairly complex. You have three stages:
a. finding out what fields have been populated
b. finding out if that's valid (two=no, 0=no, 1=yes)
c. parsing in the event of correct entry
In those stages, i would only worry about exceptions in c.
a. finding out what fields have been populated
b. finding out if that's valid (two=no, 0=no, 1=yes)
c. parsing in the event of correct entry
In those stages, i would only worry about exceptions in c.
#8
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:52 AM
Hi, thanks for your reply. Im slightly confused, are you saying that edit the code so that the try only encompasses part of what it is now?
#9
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 05:58 AM
Personally, if i were doing this, i would simplify the input scenarios with ONE field and two radio buttons in a button group marked something like £->€ and €->£. All you then have to do is to check for an empty field - perhaps not even that if you rely on step c. above to catch the exception
#10
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 06:05 AM
Hi, thanks again for all your replies. I want to keep the format of the applet pretty close to how it is. I am far from an expert in java. Is there no way to keep the try and catch there, or maybe use if /else instead? What I want really is:
if box a is empty, try box b and calculate. If box b is empty try box a and calculate. If both boxes are empty, then give an message in the console. It seems this should be simple enough, but having tried adding an if in there before, it still didnt give the right result. As in 'if (myEuroField == null);
So lost. Thanks again
if box a is empty, try box b and calculate. If box b is empty try box a and calculate. If both boxes are empty, then give an message in the console. It seems this should be simple enough, but having tried adding an if in there before, it still didnt give the right result. As in 'if (myEuroField == null);
So lost. Thanks again
#11
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 06:17 AM
Start with a. above and then post your code
#12
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 06:21 AM
g00se, on 12 May 2011 - 06:17 AM, said:
Start with a. above and then post your code
Erm, not quite sure what you mean. This is what I'm trying to do. The program runs apart from that try catch problem. Is there no way to put a condition in the existing code I have posted? Again, i'm still a java beginer.
#13
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 06:34 AM
But you're NOT doing it. You go straight into taking the input and parsing it - empty or not. What's more, you do it for BOTH fields, which doesn't make sense
Back later. btw, you can even add a FOURTH step before you get into parsing the input: determining which of the two use cases the user is concerned with
Back later. btw, you can even add a FOURTH step before you get into parsing the input: determining which of the two use cases the user is concerned with
#14
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 06:41 AM
g00se, on 12 May 2011 - 06:34 AM, said:
But you're NOT doing it. You go straight into taking the input and parsing it - empty or not. What's more, you do it for BOTH fields, which doesn't make sense
Back later. btw, you can even add a FOURTH step before you get into parsing the input: determining which of the two use cases the user is concerned with
Back later. btw, you can even add a FOURTH step before you get into parsing the input: determining which of the two use cases the user is concerned with
Right. IS there a way of adding an if statement within the try block?
#15
Re: ActionListener problem, try/catch issue
Posted 12 May 2011 - 10:11 AM
Of course. Start by finding out the state of the fields
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|