Urgent exception handeling!

Hello,

My problem is with exception handeling. I'm using java.awt. and i'm writing an application. My problem is i'm not really sure on how the format for exception handeling is.

public boolean action(Event evt, Object arg)

{

String str1 = textField1.getText();

String str2 = textField2.getText();

try

{

int int1 = Integer.parseInt(str1);

int int2 = Integer.parseInt(str2);

int answer = int1 / int2;

answerStr = String.valueOf(answer);

}

catch (NumberFormatException e)

{

answerStr = "Bad number!";

}

catch (ArithmeticException e)

{

answerStr = "Division by 0!";

}

repaint();

return true;

}

This works for text field contaning int but what if i whant to input a valid textfield containg string and int for example the input should be MH123 but instade enters 8976.

The other this is how do i implement JOptionPane into my code. Can JOptionPane be used in java.awt?

Thanks!

Your help is greatly appreaciated!

-pal_

[1107 byte] By [palv] at [2007-9-26 1:22:46]
# 1

> This works for text field contaning int but what if i

> whant to input a valid textfield containg string and

> int for example the input should be MH123 but

> instade enters 8976.

In that case, don't do Integer.parseInt(), keep it as a string, and check the string for certain characters at certain positions - see the API Javadoc for the String class

> The other this is how do i implement JOptionPane into

> my code. Can JOptionPane be used in java.awt?

Can but shouldn't! It's generally a bad idea to mix "lightweight" (ie Swing) and "heavyweight" (ie AWT) components - can lead to unexpected visual behaviour. Also, if you're coding applets, life is simpler if you don't use Swing

artntek at 2007-6-29 1:00:19 > top of Java-index,Archived Forums,New To Java Technology Archive...