Results 1 to 6 of 6
Thread: Exception Handling not working
- 05-02-2011, 12:02 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Exception Handling not working
This code has to handle exception errors gracefully. I am trying to write code in which a user inputs an index of array and it will display the element of that index. If the user inputs an index higher than 100 or non interger an exception error has to display in an text field for the user. I know I am missing something but I cannot see it. I have been working on this code for over 2 weeks and the class is almost done.
Java Code:/*this code if they input correct index they get a element, but bad index & non interger dispalys error in compiler not in the text field. */ public void actionPerformed(ActionEvent e){ String inputString; int num; inputString=indexfield.getText(); num=Integer.parseInt(inputString); try { if(num>99 ||num<0); } catch (IndexOutOfBoundsException ex) { valuefield.setText("Out of Bounds"); } catch (NumberFormatException ex) { valuefield.setText("Not a number."); } valuefield.setText(number[num]+""); }Java Code:/*This code dispalys correct element in textfield, throws index out of bounds error in compiler & in text texfield, but does not handle the non interger at all. */ public void actionPerformed(ActionEvent e){ String inputString; int num; inputString=indexfield.getText(); num=Integer.parseInt(inputString); try { if(num>99 ||num<0) throw new IndexOutOfBoundsException(""); } catch (IndexOutOfBoundsException ex) { valuefield.setText(" Out of bounds"); } catch (NumberFormatException ex) { valuefield.setText("Not a number."); } valuefield.setText(number[num]+""); }
- 05-02-2011, 12:30 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
to handle a non ingteger you should be trying to parse the value.
The problem is, if you enter the incorrect number(i.e: not a number) the number format exception is thrown, it is a runtime exception so it will just display the exception and exit.Java Code:try{ int num = Integer.parseInt("ABC"); } catch(NumberFormatException nfe){ //set text/do other stuff. }
- 05-02-2011, 01:35 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
I am a totally confused. The original input is a string, which I turn around and change it to a int so it can find it in the array. If I try using what you gave me to find a string it is getting an error of ("num is already defined in actionPerformed(java.awt.event.ActionEvent) which has defined - num=Integer.parseInt(inputString);.
Java Code:public void actionPerformed(ActionEvent e){ String inputString; int num; inputString=indexfield.getText(); num=Integer.parseInt(inputString); try { if(num>99 ||num<0) throw new IndexOutOfBoundsException(""); } catch (IndexOutOfBoundsException ex) { valuefield.setText(" Out of bounds"); } try{ int num = Integer.parseInt ("ABC"); } catch (NumberFormatException ex) { valuefield.setText("Not a number."); } valuefield.setText(number[num]+""); }
- 05-02-2011, 02:02 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You don't have to copy mine exactly, it was more of to point out that Integer.parseInt is a dangerous method(can throw a numberformatexception)
What happens if you type in 1bc. Does it spit a stack trace out? It should because number format exception is a run-time exception, if you don't catch the attempt to parse it an error can be thrown.
The line
should beJava Code:num = Integer.parseInt(inputString)
After this you can test the indexing stuff.Java Code:try{ num = Integer.parseInt(inputString); } catch(NumberFormatException nfe){ //do stuff here }
- 05-02-2011, 05:34 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 28
- Rep Power
- 0
Ok, finally got the applett to work, thank you for your help
- 05-02-2011, 05:37 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are welcome :) If you ever need any help don't hesitate to come ask questions. I am on this forum about 15-16 hours a day(I know it's sad, but I don't have much of a life atm), and I love to help people out.
Similar Threads
-
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 12:00 AM -
Exception Handling
By liljester in forum New To JavaReplies: 4Last Post: 06-21-2010, 03:09 PM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM -
JSTL Exception Handling
By chaatf in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-18-2007, 02:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks