Hi Pravglad, Check this code:

package myownjavaarrayproject;

import javax.swing.JOptionPane;

/**
 *
 * @author root
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        String askFor[]={"First Name","Last Name"};
        int max=0;
        int maxFirstName=0;

        String family[][]=new String[3][2]; //Here we store the first
name and last name

        for(int i=0;i< family.length;i++){

            // Getting the First and Last Name of three family members
            for(int j=0;j<family[i].length;j++){

                if(j==0){ // if j=0, we are asking for the first name,
else, the last name.

                    family[i][j]=JOptionPane.showInputDialog("Enter a
"+askFor[j]+" of your family member number "+(i+1)+":");
                }else{

                    family[i][j]=JOptionPane.showInputDialog("Enter a
"+askFor[j]+" of your family member "+(i+1)+":");
                }
            }

            System.out.println(family[i][0]+ " "+ family[i][1]+" has
"+family[i][0].length()+ " characters in the First Name ");
        }// End for

        // Checking what Family Member has the longest First Name

        for(int k=0;k<family.length;k++){
            if(family[k][0].length()>max){
                maxFirstName=k;
                max=family[k][0].length();
            }
        }

        // Displaying the result
        String message=family[maxFirstName][0] +"
"+family[maxFirstName][1] + " has the longer first name, with  ";
        message=message+" "+family[maxFirstName][0].length()+ "
characters.";

        System.out.println("+-----------------+");
        System.out.println(message);

        JOptionPane.showMessageDialog(null, message);

    }
}

On Oct 11, 1:02 pm, Mihai DINCA <[email protected]> wrote:
>   Hi Pravglad
>
> I'm OK with the first part. I would write it in less code, such as:
>      ...
>      String firstPerson = JOptionPane.showInputDialog( ... );
>      String secondPerson = JOptionPane.showInputDialog( ... );
>      ...
>      String[ ] firstPersonName = firstPerson.split(" ");
>      ...
> but it works perfectly in the way you did.
>
> Then, for the second part, the path you took is very complicated, I can
> hardly follow. In the big lines (assuming that len1, len2 and len3 are
> the lengths of the Strings respectively), you wrote:
>
> // First check :
> // -----------------
> if len1 > len2 then {
>      if len3 > len1 then print "The third person wins" .
>
> }
>
> // Second check (an independent one) :
> // ----------------------------------------------------
> if len1 == len2 then {
>      print "First equals Second"} else {
>
>      if len2 > len3 then {
>          print "Second wins"
>      } else print "First wins"
>
> }
>
> There are many ways to improve. One would be to presume that the first
> person may be the winner. But if the second has longer name, then the
> second might be the winner. But if the third is longer then the second
> third is the winner. Something like:
>
>     // We assume that the first is the winner
>     if len1 < len2 then { // The first cannot be the winner, but the
>     second can
>          if len2 < len3 then // The second cannot be the winner neither
>              print "Three wins"
>          else
>              print "Two wins"
>     } else { // The first still can be the winner
>          if len1 < len3 then // The third is bigger than 1 that is
>     bigger or equal to 2
>              print "Three wins"
>          else
>              print "First wins"
>     }
>
> Now, try to write this in Java instead of "pseudo code". Just one more
> suggestion: in complicated comparisons like this, try to keep the "<" or
> ">" signs in the same direction (i.e. either use only "<"s or use only
> ">"s), otherwise it is difficult to follow.
>
> Hope it helps
> Mihai
>
> Le 11/10/2010 07:24, Pravglad a �crit :
>
>
>
> > i cant seem to get this to wrong.. one or the other goes wrong..can
> > someone tlel me whats worng with this code
>
> > Display the name of the family member who has the longest first name
> > (not the longest of the  total name) as following.  (If there is a
> > tie, just display one of the two.)
>
> >   */
> > import javax.swing.*;
>
> > public class MyJavaArray {
>
> >      public static void main(String[] args) {
>
> >             // Step 1
> >          String firstPerson;
> >          String secondPerson;
> >          String thirdPerson;
>
> >          // Step 2
> >          firstPerson = JOptionPane.showInputDialog("Please enter your
> > name");
> >          secondPerson = JOptionPane.showInputDialog("Please enter your
> > name");
> >          thirdPerson = JOptionPane.showInputDialog("Please enter your
> > name");
>
> > //Step 3
> >          // creating the two arrays
> >          String[] firstPersonName = new String[2];
> >          String[] secondPersonName = new String[2];
> >          String[] thirdPersonName = new String[2];
> >          // splitting the two names into the newly created array
> >          firstPersonName = firstPerson.split(" ");
> >          secondPersonName = secondPerson.split(" ");
> >          thirdPersonName = thirdPerson.split(" ");
>
> >          // Step 4
> >          if (firstPersonName[0].length()>
> > secondPersonName[0].length() )
> >             if (firstPersonName[0].length()<
> > thirdPersonName[0].length()) {
> >                 System.out.println(thirdPersonName[0] + " has longer
> > first name than " + secondPersonName[0] + " and " +
> > firstPersonName[0]);
>
> >          } if (firstPersonName[0].length() ==
> > secondPersonName[0].length()) {
> >                 System.out.println(secondPersonName[0] + "'s first name
> > is the same length as " + firstPersonName[0] + "'s first name.");
> >           } else if (firstPersonName[0].length() ==
> > thirdPersonName[0].length()) {
> >                 System.out.println(thirdPersonName[0] + "'s first name
> > is the same length as " + firstPersonName[0] + "'s first name.");
> >      } else if (secondPersonName[0].length()>
> > thirdPersonName[0].length()) {
> >                 System.out.println(secondPersonName[0] + " has longer
> > first name than " + thirdPersonName[0] + " and " +
> > firstPersonName[0]);
> >            } else
> >                 System.out.println(firstPersonName[0] + " has longer
> > name than  " + secondPersonName[0] + " and " + thirdPersonName[0]);
>
> >          }
> >      }

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