Hi Rob
I would put the "totalnumber" befor the "String[]" declaration, in order
to get "String[] nameArrayForPerson = new String[totalnumber];".
Arrays have a definitely clear length from the beginning (unlike some
other languages). It is possible to have array-like structured data
allowing to dynamically modify the length, but you must use in this case
some Collections. There is a reason for it: "static length" arrays are
fast, "dynamical length" arrays are slower. The programmer can choose
the best compromise according to the different constraints.
Then, pay attention: "String" begins with a capital letter. Java makes a
difference between capitals and lower cases. "String" is a classical
problem as using I don't know which font "String" and "string" look alike.
Then, "nameArrayForPerson[counter]" is a String, not an int, so you
don't need to use "Integer.parseInt(...)". "Integer.parseInt(...)"
converts a String (like "123") to an int (like 123). But the user enters
a String name, such as "John Smith" and it has to be stored as a String.
You will need to use the "length()" method of the String to get its
length, something like "nameArrayForPerson[counter].length()". The
length is an integer (int), and you can compare lengths. But, before,
you must split the String in order to pull apart the first name from the
last name and to compare only the length of one of them (I don't
remember which one is requested). You split a String using its method
"split(...)". The exercise consist in discovering the methods of the
"String" class.
Hope it helps
mihai
[email protected] a écrit :
I'm at a loss for what to do with the homework. Here is what I have
so far with the corresponding output.
import javax.swing.JOptionPane;
public class OwnJavaArray {
public static void main(String[] args) {
string[] nameArrayForPerson = new string[];
int counter;
int max = 0;
int totalnumber = 3;
// Prompt a user to enter numbers
for(counter = 0; counter < totalnumber; counter++){
nameArrayForPerson[counter] = Integer.parseInt
(JOptionPane.showInputDialog("Enter the family
names until " + totalnumber + " sets of first and last names are
entered"));
// Compute the greatest name up to this point
if((counter == 0)||(name[counter] > max))
max = nameArrayForPerson[counter];
String[] nameArrayForPerson[counter] =
person[counter]NameInstance.split(" ");
}
// Display the name with the greatest length.
JOptionPane.showMessageDialog(null,"The name with the greatest
length is " + max);
}
init:
deps-jar:
Compiling 1 source file to
C:\Users\Owner\Documents\NetBeansProjects\MyInputFromKeyboardJOptionPaneProject\MyOwnJavaArrayProject\build\classes
C:\Users\Owner\Documents\NetBeansProjects\MyInputFromKeyboardJOptionPaneProject\MyOwnJavaArrayProject\src\OwnJavaArray.java:6:
array dimension missing
string[] nameArrayForPerson = new string[];
C:\Users\Owner\Documents\NetBeansProjects\MyInputFromKeyboardJOptionPaneProject\MyOwnJavaArrayProject\src\OwnJavaArray.java:21:
']' expected
String[] nameArrayForPerson[counter] =
person[counter]NameInstance.split(" ");
C:\Users\Owner\Documents\NetBeansProjects\MyInputFromKeyboardJOptionPaneProject\MyOwnJavaArrayProject\src\OwnJavaArray.java:21:
illegal start of expression
String[] nameArrayForPerson[counter] =
person[counter]NameInstance.split(" ");
C:\Users\Owner\Documents\NetBeansProjects\MyInputFromKeyboardJOptionPaneProject\MyOwnJavaArrayProject\src\OwnJavaArray.java:21:
';' expected
String[] nameArrayForPerson[counter] =
person[counter]NameInstance.split(" ");
C:\Users\Owner\Documents\NetBeansProjects\MyInputFromKeyboardJOptionPaneProject\MyOwnJavaArrayProject\src\OwnJavaArray.java:27:
reached end of file while parsing
}
Thanks in advance.
Rob B.
--
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 unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to
this email with the words "REMOVE ME" as the subject.
--
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 unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscribegooglegroups.com or reply to this email with the
words "REMOVE ME" as the subject.