Hi,
I made a little change to Sudhir's code...
/**
*
* @author Salman
*/
import javax.swing.JOptionPane;
public class JavaFirstNameLength {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String name[] = new String[10];
// String firstName[] = new String[6];
int count=Integer.parseInt(JOptionPane.showInputDialog("How
many names would you like to compare"));
String max = "";
for (int i=0;i<count;i++)
{
name[i] = JOptionPane.showInputDialog("Enter " +(++i)+ "
name of " +count+ " names");
--i;
}
for (int j=0;j<count;j++)
{
String firstName[] = name[j].split(" ");
if((j==0)||(max.length()<firstName[0].length()))
{
max = firstName[0];
}
}
JOptionPane.showMessageDialog(null, "The longest first name is
"+max);
}
}
On Jul 23, 1:59 am, Tommy Anderson <[email protected]> wrote:
> Dear Sudhir,
>
> Yes, glad you got it. I realize that's right, because:
>
> name[j].split(" ")
>
> will create an array of String, and the first string will be the first
> string in the array... So you should use "firstName[0]" not "firstName"...
>
> But maybe you could change the name of the variable!
>
> Tommy
>
> 2009/7/23 sudhir Ganesh Paluru <[email protected]>
>
>
>
> > 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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---