2009/7/21 sudhir Ganesh Paluru <[email protected]>
> Hello All,
>
> I am trying out 1036 homework and have the following issue
>
> code :
> _____
>
> import javax.swing.JOptionPane;
> public class ArrayHomeProject {
>
> /**
> * @param args the command line arguments
> */
> public static void main(String[] args) {
> // TODO code application logic here
> String name[] = new String[3];
> // String firstName[] = new String[6];
> String max = "";
> for (int i=0;i<3;i++)
> {
> name[i] = JOptionPane.showInputDialog("Enter threee names"+i);
> }
> for (int j=0;j<3;j++)
> {
> String firstName[] = name[j].split(" ");
> if((j==0)||(max.length()>firstName[j].length()))
> {
> max = firstName[j];
> }
> }
> System.out.println("The longest first name is"+max);
> }
>
> }
>
> Error Message when i run it:
> ___________________________
>
> run:
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
> at ArrayHomeProject.main(ArrayHomeProject.java:28)
> Java Result: 1
>
> Any pointers would be appreciated.
>
> Thanks,
> Sudhir
Hi
Your problem is in the following line:
if((j==0)||(max.length()>firstName[j].length()))
because the array firstName has only one position (firstName[0]) so when j=1
is the error
so the solution should be the next:
if((j==0)||(max.length()<firstName[0].length()))
{
max = firstName[0];
}
Hope that helps
Yamileth Echeverria
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---