Hi James
First of all your else clause is never executed because i is always smaller
than members (I<member) the use of if else clause is not right here. What
you wont to do is to keep asking WHILE i is smaller than members...
Change your if clause to something like...
while i is smaller than members do
ask for members name //increase i with i++ inside the array (names[i++])
the else clause can be omitted totally...
Take out the ...
else { and the matching } and your code work perfectly
My best regards
- Halli
2009/9/17 James <[email protected]>
>
> 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
-~----------~----~----~----~------~----~------~--~---