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