Hi Victor
I don't understant all your choices from the code you sent:
*
*
*String[] namePerson1 = new String[2];
// Why is this an array of Strings?
// I would use a simple String to get the first name/last name
// Same comment for the following two variables
String[] namePerson2 = new String[2];
String[] namePerson3 = new String[2];*
int i =0;
*
// The following three loops will ask you 6 times the first/last name
// Twice for each of the arrays namePerson1 to
namePerson3*
// creates a loop for the user to insert first name
for( i=0; i<namePerson1.length; i++){
namePerson1[i] = JOptionPane.showInputDialog
("Please Enter First and Last Name ");
break;
}
// For loop to insert second name
for ( i= 0; i< namePerson2.length; i++){
namePerson2[i]=JOptionPane.showInputDialog
("Please Enter 2nd First and Last Name ");
break;
}
for( i =0; i<namePerson3.length; i++){
namePerson3[i]=JOptionPane.showInputDialog
(" Please Enter 3rd First and Last Name ");
break;
}
*// Which is the value of "i"? It is zero?
// Then it will split only the first element of
each of the three arrays of Strings
// namePerson1 .. namePerson3 (the user entered
three names for nothing,
// only three of the six entered names are used)
//
// The split("") cannot work properly. You must
mean split(" ") (with a space in it)
// in order to separate the first name from the
last name*
//obtain the first name in array using split method
String[] firstName1 = namePerson1[*i*].split("");
String[] firstName2 = namePerson2[*i*].split("");
String[] firstName3 = namePerson3[*i*].split("");
//get the lengths of first names using length method of
String Class
int lengthOfName1 = firstName1[0].length();
int lengthOfName2 = firstName2[0].length();
int lengthOfName3 = firstName3[0].length();
// compare the length and returns the longest first name
//display the results
*
// Of course, it will not work if any two names have the same length
// In fact, they HAVE the same length, as split("")
(no space) splits the String
// in one-letter strings containing all the
letter from the original String*
if((lengthOfName1 > lengthOfName2) && (lengthOfName1 >
lengthOfName3))
System.out.println(" Longest first name entered is "
+ firstName1[0] );
else if((lengthOfName2 > lengthOfName1) &&(lengthOfName2 >
lengthOfName3))
System.out.println(" Longest first name entered is "
+ firstName2[0]);
else if ((lengthOfName3 > lengthOfName1)&&(lengthOfName3 >
lengthOfName2))
System.out.println(" Longest first name entered is "
+ firstName3[0]);
//else
//System.out.println("Maybe 2 or more names have the
same length");
// last line
System.out.println(firstName1);*// It will just write that firstName
is an array of Strings*
In brief:
--> The user enters :
(1) Mickey Mouse --> namePerson1[0]
(2) Abraham Lincoln --> namePerson1[1]
(3) Donald Duck --> namePerson2[0]
(4) George Washington --> namePerson2[1]
(5) Luke Skywalker --> namePerson3[0]
(6) Darth Vador --> namePerson3[1]
--> Then the Strings (1), (3) and (5) are split, as the "i" variable has
a value of zero:
firstName1 = {"M","i","c","k","e","y"," ","M","o","u","s","e"}
firstName2 = {"D","o","n","a","l","d",...
--> Then the "length" are computed:
lengthOfName1 = "M".length() = 1
lengthOfName2 = "D".length() = 1
lengthOfName3 = "L".length() = 1
--> The length are compared, then, as no one is greater than the others,
nothing is written.
I would use
String namePerson1; // Simple String and not an array
...
String namePerson2;
or, even better:
String[] namePerson = new String[3]; // The th)ree Strings the user
will enter
In the latest case, a loop will allow to read all three names:
for ( int i = 0 ; i < namePerson.length ; i++ )
namePerson[i] = JOptionPane.showInputDialog("Please enter first
name and last name for " + i);
In the simplest case, the "firstName" is also an array. Each of the
three elements of the array is an array of Strings (containing first
name/last name):
String[][] firstName = new String[3][];
A loop allows to fiill the values:
for ( int i = 0 ; i < namePerson.length ; i++ )
firstName[i] = namePerson[i].split(" "); // One space between
the doublequotes
Of course, you can compute the lengths:
int[] lengthOfName = new int[3];
for ( int i = 0 ; i < firstName.length ; i++ )
lengthOfName[i] = firstName[i][0].length();
The algorithm should print something, even if there are some equal names.
For e.g.:
String result;
if ( lengthOfName[0] > lengthOfName[1]){ // 0 is bigger than 1
if ( lengthOfName[0] > lengthOfName[2]) // 0 is bigger than 2 too
result = lengthOfName[0];
else
result = lengthOfName[2]; // 0 is bigger than 1 but 2 is
bigger than 1 (or equal)
} else { // 0 is not bigger than 1 (1 is bigger or equal to 0)
if ( lengthOfName[1] > lengthOfName[2] ) // 1 >= 0 & 1 > 2
result = lengthOfName[1];
else
result = lengthOfName[2]; // 1>=0 && 2>=1
}
System.out.println("The longest first name is (probably) " + result );
A better algorithm is iterating (so it may work with no matter how many
first names).
You can assume, for e.g. that the first of the first names is the
longest. The make a loop that scans all the others and changes the
result if an even longer name is found:
String result = firstName[0][0];
for ( int i = 1 ; i < lengthOfName.length ; i++ ){ // it starts
from "1"
if ( result.length() < lengthOfName[i].length())
result = firstName[i][0];
}
System.out.println( "The longest name is (probably) " + result );
Hope it helps
Mihai
Le 27/01/2011 13:20, victor bruno a écrit :
Hi fellows! Hope everybody is doing great.
I'm solving the java Array lab and this far wrote the following code:
String[] namePerson1 = new String[2];
String[] namePerson2 = new String[2];
String[] namePerson3 = new String[2];
int i =0;
// creates a loop for the user to insert first name
for( i=0; i<namePerson1.length; i++){
namePerson1[i] = JOptionPane.showInputDialog
("Please Enter First and Last Name ");
break;
}
// For loop to insert second name
for ( i= 0; i< namePerson2.length; i++){
namePerson2[i]=JOptionPane.showInputDialog
("Please Enter 2nd First and Last Name ");
break;
}
for( i =0; i<namePerson3.length; i++){
namePerson3[i]=JOptionPane.showInputDialog
(" Please Enter 3rd First and Last Name ");
break;
}
//obtain the first name in array using split method
String[] firstName1 = namePerson1[i].split("");
String[] firstName2 = namePerson2[i].split("");
String[] firstName3 = namePerson3[i].split("");
//get the lengths of first names using length method of String
Class
int lengthOfName1 = firstName1[0].length();
int lengthOfName2 = firstName2[0].length();
int lengthOfName3 = firstName3[0].length();
// compare the length and returns the longest first name
//display the results
if((lengthOfName1 > lengthOfName2) && (lengthOfName1 >
lengthOfName3))
System.out.println(" Longest first name entered is "
+ firstName1[0] );
else if((lengthOfName2 > lengthOfName1) &&(lengthOfName2 >
lengthOfName3))
System.out.println(" Longest first name entered is "
+ firstName2[0]);
else if ((lengthOfName3 > lengthOfName1)&&(lengthOfName3 >
lengthOfName2))
System.out.println(" Longest first name entered is "
+ firstName3[0]);
//else
//System.out.println("Maybe 2 or more names have the same
length");
// last line
System.out.println(firstName1);
And it has no output.The last line I inserted just to test the code,as
a result I got the following output: [Ljava.lang.String;@a0dcd9.
What's wrong please?
Thank you,
--
To post to this group, send email to
javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en