array problem

I think I solved your problem.

Try this.

remember you want the else to happen just before the last array element.

Here is sample code

 

public class ShowArray {

                public static void main (String args[])

                {

                                String myArray[] = {"one","two","three"};

for (int x = 0;x <= myArray.length-1;++x){

                System.out.print(myArray[x]);

if (x < myArray.length-2)

//array minus two is the element before the last one.

System.out.println(", ");

else

System.out.print(" and ");

}

 

 

 

 

                }

}

 

 

Other related posts: