Hi
Your code miss just the part where the entered name is assigned to an
element of the array:
...
for ( counter = 0 ; counter < names.length ; counter++ ){
*names[counter] = *JOptionPane.showInputDialog("Please enter
your name");
}
...
For dealing with the arrays, let's say you have to define several
similar variables in your program:
...
// Definition
String name0 = null; // first
String name1 = null; // second
String name2 = null; // third
String name3 = null; // fourth
...
You store a new value in the variable with the subscript 2 (the third one):
...
// Assign
name2 = "John Brown";
...
You pick the value stored (in order to display it for e.g.):
...
// Get the value
System.out.println(name2);
...
You print all the value of all the 4 variables with:
...
// Get all the values
System.out.println(name0);
System.out.println(name1);
System.out.println(name2);
System.out.println(name3);
...
Instead of writing so much, you can do the same thing like this:
...
// Definition
String name = new String[4];
/* Only one line instead of:
** String name0 = null;
** String name1 = null;
** String name2 = null;
** String name3 = null;
*/
...
// Assign
name[2] = "John Brown"; // name2 = "John Brown";
...
// Get the value
System.out.println(name[2]); // System.out.println(name2);
...
// Get all the values
for ( int i = 0 ; i < name.length ; i++ )
System.out.println(name[i]);
/* Instead of the four lines:
** System.out.println(name0);
** System.out.println(name1);
** System.out.println(name2);
** System.out.println(name3);
*/
...
The only difficulty is to remember that the first element has the
subscript "0" and the last element of an array of N elements has the
subscript N-1. All the rest comes quickly.
Hope it helps
mihai
Jason Waite a écrit :
I'm really struggling with arrays. The concept I understand, but
pulling it all together has been a challenge.
I have this so far for enter three names and comparing the lengths of
the first names to determine which is the longest.
public static void main(String[] args) {
//declare and create a String array to hold three names
String[] names = new String[3];
int counter;
for (counter=0;counter<names.
length;counter++){
JOptionPane.showInputDialog("Please enter your name");
How do I make sure the names entered are assigned to elements within
the array? My problem is determining the correct way to compare the
names once they are entered. Once the names are entered they aren't
stored as anything, or are they?
Thanks in advance to all!
Jason
--
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
--
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