Daniel try my code below and then compare the two and see where you might be having trouble at.


import javax.swing.JOptionPane;


public class SplitName {

    /**
     * @param args
     */
    public static void main(String[] args) {
       
       
        // Step 1
        String firstPerson;
        String secondPerson;

        // Step 2
        firstPerson = JOptionPane.showInputDialog("Please enter your name");
        secondPerson = JOptionPane.showInputDialog("Please enter your name");

        // Step 3
        // creating the two arrays
        String[] firstPersonName = new String[2];
        String[] secondPersonName = new String[2];
        // splitting the two names into the newly created array
        firstPersonName = firstPerson.split(" ");
        secondPersonName = secondPerson.split(" ");

        // Step 4
        if (firstPersonName[0].length() > secondPersonName[0].length()) {
               System.out.println(firstPersonName[0] + " has longer first name than " + secondPersonName[0]);
        } else 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 {
            System.out.println(secondPersonName[0] + " has longer first name than " + firstPersonName[0]);
           
        }
    }

}



Stephen










Ruben wrote:
the personNameInstance is for contain the name of the person and this
variables is not initialized with the names of persons (you need load
the names in the dialog box in this variables), and the
nameArrayForPerson is for split the first and the second name in this
array of string, look this code:

String person1NameInstance,person2NameInstance;
String[] nameArrayForPerson1;
String[] nameArrayForPerson2;

person1NameInstance = JOptionPane.showInputDialog("Please enter your
name");
person2NameInstance = JOptionPane.showInputDialog("Please enter your
name");

nameArrayForPerson1 = person1NameInstance.split(" ");
nameArrayForPerson2 = person2NameInstance.split(" ");

// Get the lengths of strings using length() instance (non-static)
method
int lengthOfFirstNameOfPerson1 = nameArrayForPerson1[0].length();
int lengthOfFirstNameOfPerson2 = nameArrayForPerson2[0].length();


        // Compare the lengths of the first names between person1 and
person2
if (lengthOfFirstNameOfPerson1 > lengthOfFirstNameOfPerson2){
   System.out.println(nameArrayForPerson1[0] +
                              " has longer first name than " +
                              nameArrayForPerson2[0]);
}

else
    System.out.println(nameArrayForPerson2[0] +
                              " has longer first name than " +
                              nameArrayForPerson1[0]);

    }

On Jan 8, 10:49 pm, Daniel Mays <[email protected]> wrote:
  
This is my code:

String[] nameArrayForPerson1 = person1NameInstance.split(" ");
String[] nameArrayForPerson2 = person2NameInstance.split(" ");

// Get the lengths of strings using length() instance (non-static) method
int lengthOfFirstNameOfPerson1 = nameArrayForPerson1[0].length();
int lengthOfFirstNameOfPerson2 = nameArrayForPerson2[0].length();

  nameArrayForPerson1 = JOptionPane.showInputDialog("Please enter your
name");
  nameArrayForPerson2 = JOptionPane.showInputDialog("Please enter your
name");

        // Compare the lengths of the first names between person1 and
person2
if (lengthOfFirstNameOfPerson1 > lengthOfFirstNameOfPerson2){
   System.out.println(nameArrayForPerson1[0] +
                              " has longer first name than " +
                              nameArrayForPerson2[0]);}

else
    System.out.println(nameArrayForPerson2[0] +
                              " has longer first name than " +
                              nameArrayForPerson1[0]);

    }

I am a little confused because nothing that I have tried is not worked
    

Reply via email to