Hi,
Bit late joining the course so trying to catch up!
I'm on the JavaArray homework from LAB-1036 but I'm having some
problems. It looks ok to me, but when I run I get asked my name and
then it finished. No error messages though. Have I used split right?
And is there anyway to keep all the split names in a single array?
e.g. [first][last]
Cheers,
James
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.swing.JOptionPane;
/**
*
* @author bmb3jkrk
*/
public class OwnJavaArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Declare counter and number of names to be entered (members)
int i=0;
int members=3;
String[] names = new String[members];
//Input name
if(i<members){i++;
names[i]=JOptionPane.showInputDialog("What is your name?");}
else{
// Get the first name from a name string using split() instance
(non-static) method
String[] nameArray0 = names[0].split(" ");
String[] nameArray1 = names[1].split(" ");
String[] nameArray2 = names[2].split(" ");
// String[] nameArrayForPerson2 = person2NameInstance.split
(" ");
// Get the lengths of strings using length() instance (non-
static) method
int lengthFirstName0 = nameArray0[0].length();
int lengthFirstName1 = nameArray1[0].length();
int lengthFirstName2 = nameArray2[0].length();
// Compare the lengths of the first names between person1
and person2
if (lengthFirstName0 > lengthFirstName1){
System.out.println(nameArray0[0] +
" has longer first name than " +
nameArray1[0]);
}
if(lengthFirstName1>lengthFirstName2){
System.out.println(nameArray1[0]+" has longer first
name than "+nameArray2[0]);}
else{System.out.println(nameArray2[0]+" has the longest
first name.");}
}}}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---