On May 1, 10:11 am, franzgabriel <[email protected]> wrote:
> somebody help me please explain this in homework 1036 Java array ,
> please?
>
> //Get the first name from a name string using split() instance (non-
> static) method
> String[] nameArrayForPerson1 = person1NameInstance.split(" ");
> String[] nameArrayForPerson2 = person2NameInstance.split(" ");
You should read the doc for split in String class. See <http://
java.sun.com/javase/6/docs/api/>
Here it splits the person1NameInstance variable in to parts: the one
before a space, and the one after a space, provided that there is a
space in the person1NameInstance variable. Note that the method
returns an array of String.
So, say your person1NameInstance contains: John Doe, it returns an
array: ["John", "Doe"].
Here you have put this array into nameArrayForPerson1, so that after
splitting:
nameArrayForPerson1 = ["John","Doe"];
>
> // Get the lengths of strings using length() instance (non-static)
> method
> int lengthOfFirstNameOfPerson1 = nameArrayForPerson1[0].length();
> int lengthOfFirstNameOfPerson2 = nameArrayForPerson2[0].length();
Here, to understand what happens you should go step by step.
1 - First step
What is nameArrayForPerson1[0]?
As with any array, this is the first element of the array.
In the example I've given, it is: "John"
You may want to revise a bit the Array class.
2 - Second step
So, you've got a string with the first step, now you should read the
doc for length method of String class.
To simplify a bit, it gives the number of characters in the string.
Here it returns 4, which is the number of characters in the string
"John".
So now you have:
lengthOfFirstNameOfPerson1 = 4;
After doing the same thing for nameArrayForPerson2, if your second
person is named: "Victor Jump", you get:
lengthOfFirstNameOfPerson2 = 6;
>
> // 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]);
>
> }
And now you merely compare 4 with 6.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---