It's easyer than it looks. Everything instead of the "String.split" method is in the lessons preceding this homework. You can check how to use this method in the "javadoc".
Anyway, try to understand this code:

public class OwnJavaArray {

    public static void main(String[] args) {
       
        String fullNames[]=new String[3];
       
        for(int i=0;i<3;i++){
        fullNames[i] = JOptionPane.showInputDialog(null,"Enter full name of person number " + (i+1));
        }
       
        String[] Name1 = fullNames[0].split(" ");
        String[] Name2 = fullNames[1].split(" ");
        String[] Name3 = fullNames[2].split(" ");
       
        int length1=Name1[0].length();
        int length2=Name2[0].length();
        int length3=Name3[0].length();

        int i=0;

        if ((length1>=length2)&(length1>=length3)){
            i=0;
        }
        if ((length2>=length3)&(length2>=length1)){
            i=1;
        }
        if ((length3>=length2)&(length3>=length1)){
            i=2;
        }

        JOptionPane.showMessageDialog(null,"The family member with a longest first name is " + fullNames[i]);
    }
}



En/na Dasarathy Raghavan ha escrit:
Please take a look at the class below.. it does not quite match the homework .. but you should be able to get the idea of what I was trying to do

I am sure we can specify and use Key=>Value pairs here to optimize this .... but I am not at the point where I can use it.

Cheers
Dasa

public class LongestLastName {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int FamilyMembers = 4;
String FamilyMemberNames[] = new String[FamilyMembers];
String FirstNames[] = new String[FamilyMembers];
String LastNames[] = new String[FamilyMembers];
String nameArray[] = new String[2];
String msg;
int nameLength = 0;
int FirstNamesLength[] = new int[FamilyMembers];
int LastNamesLength[] = new int[FamilyMembers];
int IndexOfHighestLength=0;
int LengthOfName=0;
for (int i = 0; i < FamilyMembers; i++){
do{
FamilyMemberNames[i] = JOptionPane.showInputDialog("enter "+ FamilyMembers+" names as \"FirstName LastName\"");
try {
nameLength=FamilyMemberNames[i].split(" ").length;
} catch (Exception e) {
msg="Names should be in \"FirstName LastName\" Format";
JOptionPane.showMessageDialog(null, msg);
}
}while (nameLength != 2);
}
for (int j=0; j<FamilyMemberNames.length; j++){
   nameArray=FamilyMemberNames[j].split(" ");
   FirstNames[j]=nameArray[0];
   LastNames[j]=nameArray[1];
}
for (int i=0; i<FirstNames.length;i++){
FirstNamesLength[i]=FirstNames[i].length();
if ((i==0)||(LengthOfName<FirstNamesLength[i])){
IndexOfHighestLength=i;
LengthOfName = FirstNamesLength[i];
}
}
msg="The longest First name is "+FirstNames[IndexOfHighestLength]+". Its length is "+FirstNamesLength[IndexOfHighestLength];
System.out.println(msg);
LengthOfName=0;
IndexOfHighestLength=0;
for (int i=0; i<LastNames.length;i++){
LastNamesLength[i]=LastNames[i].length();

if ((i==0)||(LengthOfName<LastNamesLength[i])){
IndexOfHighestLength=i;
LengthOfName = LastNamesLength[i];
}
}
msg="The longest last name is "+LastNames[IndexOfHighestLength]+". Its length is "+LastNamesLength[IndexOfHighestLength];
System.out.println(msg);
}

}


On Thu, Nov 13, 2008 at 9:14 AM, suganthi m <[EMAIL PROTECTED]> wrote:
Please anyone help to do the Homework exercise  MyOwnJavaArrayProject  under Java Array (jedi, 1.07)







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

begin:vcard
fn:Josep Maria Sans
n:Sans;Josep Maria
org;quoted-printable:ADOS - Serveis d'electr=C3=B2nica
adr;quoted-printable:;;P=C3=A7a. Everest vuitanta-cinc, 3;Taradell;Barcelona;08552;SPAIN
email;internet:[EMAIL PROTECTED]
tel;work:0034 93 880 12 89
tel;cell:0034 636 22 79 24
url:www.adoselectronica.com
version:2.1
end:vcard

Reply via email to