Hello All,
I got the program to work.. Thank you all.
Most of you were right when you said I had to use firstName[0] always. That
was causing the out of bounds issue.
Working 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[0].length()))
{
max = firstName[0];
}
}
System.out.println("The longest first name is "+max);
}
}
Thanks,
Sudhir
Free Java Programming Online Training Course By Sang Shin <java
On Wed, Jul 22, 2009 at 3:09 AM, Rita <[email protected]> wrote:
>
> Hi Sudhir,
>
> The split method returns an array of Strings which is assigned to your
> array firstname[]. If the name you entered consists of only a first
> name, the string array returned has the length 1. If you entered first
> and last name, the length would be 2.
> Now in the line "if ((j == 0) || ..." your counter j will allways take
> values from 0 to 2. This may be more than the length of the array and
> that causes your array-out-of-bound-exception.
>
> hth, Rita
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---