Results 1 to 6 of 6
  1. #1
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default 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]+"");
    	}

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    to handle a non ingteger you should be trying to parse the value.

    Java Code:
    try{
      int num = Integer.parseInt("ABC");
    } catch(NumberFormatException nfe){
      //set text/do other stuff.
    }
    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.

  3. #3
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    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]+"");
    	}

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    num = Integer.parseInt(inputString)
    should be

    Java Code:
    try{
      num = Integer.parseInt(inputString);
    } catch(NumberFormatException nfe){
      //do stuff here
    }
    After this you can test the indexing stuff.

  5. #5
    collwill is offline Member
    Join Date
    Mar 2011
    Posts
    28
    Rep Power
    0

    Default

    Ok, finally got the applett to work, thank you for your help

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  1. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 01:00 AM
  2. Exception Handling
    By liljester in forum New To Java
    Replies: 4
    Last Post: 06-21-2010, 04:09 PM
  3. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 10:23 AM
  4. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 04:13 AM
  5. JSTL Exception Handling
    By chaatf in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2007, 03:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •