Hi,

you have created an array that can contain 3 values:

String name[] = new String[3];

However your loop run 4 times: 

  for (int i=0;i<3;i++)

When it tries to perform the following code:

 name[3] = JOptionPane.showInputDialog("Enter threee names"+3);

it comes to an error because the highest value in the array is name[2].

So your loop should look like this:

for (int i=0;i<2;i++)


Hope that helps.


Regards,
Dian



________________________________
Von: sudhir Ganesh Paluru <[email protected]>
An: [email protected]
Gesendet: Mittwoch, den 22. Juli 2009, 01:25:08 Uhr
Betreff: [java programming] Question on 1036

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







      
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to