14 Replies - 1695 Views - Last Post: 12 May 2011 - 10:11 AM Rate Topic: -----

#1 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

ActionListener problem, try/catch issue

Posted 12 May 2011 - 05:07 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: ActionListener problem, try/catch issue

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: ActionListener problem, try/catch issue

Posted 12 May 2011 - 05:10 AM

Please post code
Was This Post Helpful? 0
  • +
  • -

#3 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

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!!!");
}
}
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#5 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

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).
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

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.
Was This Post Helpful? 1
  • +
  • -

#8 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

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?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

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
Was This Post Helpful? 0
  • +
  • -

#10 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

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
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: ActionListener problem, try/catch issue

Posted 12 May 2011 - 06:17 AM

Start with a. above and then post your code
Was This Post Helpful? 0
  • +
  • -

#12 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

Re: ActionListener problem, try/catch issue

Posted 12 May 2011 - 06:21 AM

View Postg00se, 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. :) thanks
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

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
Was This Post Helpful? 0
  • +
  • -

#14 Jimbob123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-May 11

Re: ActionListener problem, try/catch issue

Posted 12 May 2011 - 06:41 AM

View Postg00se, 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

Right. IS there a way of adding an if statement within the try block?
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: ActionListener problem, try/catch issue

Posted 12 May 2011 - 10:11 AM

Of course. Start by finding out the state of the fields
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1