Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cuziwow

macrumors newbie
Original poster
Apr 11, 2010
2
0
after i put in the for loop, one of my exception handling stop working; how can i fix it
Code:
import javax.swing.*;
public class Testnum
{
    public static void main(String []args)
    {
               char []upper = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        char []lower = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        String []place= {"first","second","third","fourth","fithth","sixth","seventh","eighth","ninth","ten","Eleventh", 
"Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth","Seventeenth", "eighteenth","ninteenth","twentieth","21st","22nd","23rd","24th","25th","26th"};    
 try{

String input = JOptionPane.showInputDialog("Enter a character or click 'Cancel' to end");
int length=input.length();
for(int j=0; j<length; j++){ [COLOR="DarkGreen"]//the cause[/COLOR]
char num=input.charAt(j);
 if (Character.isLetter(num) )
    for(int i =0; i<upper.length; i++)
        {
            if(Character.isUpperCase(num))
            {
                
                if(upper[i] ==num)
                {
                    System.out.println(num+" is Upper case");
                    System.out.println(num +" is the "+(place[i]) +" in the alphabet");
                }
            }
            else
            {
                
                if(lower[i] ==num)
                {
                    System.out.println(num+" is Lower case");
                    System.out.println(num +" is the "+(place[i]) +" in the alphabet");
                }
            
        }

    }
else if (Character.isDigit(num)){
  System.out.println(num+ " is a number"); 
}
else{
 System.out.println(num+ " is neither a number or a letter");  
}
}  
}
catch (NullPointerException aob){
   

   System.out.print("Program ended");
System.exit(0);
}
catch (StringIndexOutOfBoundsException outof){ [COLOR="DarkGreen"]//This catch doesn't work[/COLOR]
  
  System.out.print("No input letter: program will end");
System.exit(0); 



}



}
}
 
Two things to think about if the user doesn't enter a letter,

1. What happens to the for loop?

2. What throws the StringIndexOutOfBoundsException because something has to throw the exception so it can be caught?
 
uhh...what? :D can you tell me why is it like that? if i know the reason i might be able to fix it myself
 
uhh...what? :D can you tell me why is it like that? if i know the reason i might be able to fix it myself

If the user does not input anything, the input.length() function will return 0 so your length variable will be 0. What happens to the for loop if length is 0?
 
If the user does not input anything, the input.length() function will return 0 so your length variable will be 0. What happens to the for loop if length is 0?
The for loop never runs in that case... which means the index will be set to its default value. If that default happens to be -1 or some other index that will never be valid, and you then try to extract a character from the string at that position... BAM! StringIndexOutOfBoundsException will occur. Fortunately, you can prevent this from happening if you validate the input first - for example, making sure the length of the input string is greater than 0 before trying to process it. If the user entered something that makes no sense, then display an error message and exit gracefully - usually this is done with System.exit() and a non-zero argument.
 
The for loop never runs in that case... which means the index will be set to its default value. If that default happens to be -1 or some other index that will never be valid, and you then try to extract a character from the string at that position... BAM! StringIndexOutOfBoundsException will occur. Fortunately, you can prevent this from happening if you validate the input first - for example, making sure the length of the input string is greater than 0 before trying to process it. If the user entered something that makes no sense, then display an error message and exit gracefully - usually this is done with System.exit() and a non-zero argument.

Yes, that is correct. I was trying to help the OP without telling the OP the answer.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.