I have this array

Array myArray = {1,2,3}; 

I want to print the output of the array as follwoing
1,2,3

but when I do this

for(int i=0 ; i < myArray.length ; i++){
System.out.print(myArray[i]+",");
}

however I get this output 1,2,3, how can I get rid of the last comma

Member Avatar for gowans07

easy way is to put an if statement in.

if (i == (myArray.length - 1){
    System.out.print(myArray[i]);
}
else{
System.out.print(myArray[i]+",");
}

what if I want the output to be someting like this
The follwoing Joe,Mike,Will Are Students
if I do something like this

  String[] myArray = {"Joe", "Mike", "Will"};
        String a = "";
        for (int i = 0; i < myArray.length; i++) {

            if (i == (myArray.length - 1)) {
                 a = myArray[i];

            } else {
                System.out.print("The follwoing " + myArray[i] + "," + a + " Are students");
            }
            // System.out.print(myArray[i]);
        }

I get this result

The follwoing Joe, Are studentsThe follwoing Mike, Are students
and I want this output
The follwoing Joe,Mike,Will Are Students

Member Avatar for gowans07
if (i==0)
{
    System.out.print ("The following " + myArray[i])'
else if((i == (myArray.length - 1){
    System.out.print(myArray[i] + " are students");
    }
else
{
    System.out.print("," + myArray[i]);
}

Thank you gowans07 is there away for me to save the Strings in variable and print them once

Member Avatar for gowans07
String s = "";

if (i==0)
{
    s = ("The following " + myArray[i]);
else if((i == (myArray.length - 1){
    s += (myArray[i] + " are students");
    }
else
{
    s += ("," + myArray[i]);
}

System.out.println (s);

It's certainly legal Java to append strings like that, but horribly inefficient - every + or += creates a new String object.
When you want to build up a String like that you should use a StringBuilder, which is a mutable class with methods like append(...) that are very efficient. When the StringBuilder finally contains the whole thing, call its toString() method to get a single immutable String with the same contents, eg

        StringBuilder sb = new StringBuilder();

        sb.append("The following ");
        sb.append(myArray[0]);
        for (int i = 1; i < myArray.length; i++) {
            sb.append(",");
            sb.append(myArray[i]);
        }
        sb.append(" are students\n");

        String result = sb.toString();
for(int i=0 ; i < myArray.length ; i++)
      System.out.print(myArray[i]);
      for(int j=i;j<=myArray.length-i;j++ )
            {
             System.out.print(",");
            break;
            }
            }

Hi,

To not complicate things and to avoid adding an if..else you can try to stop the loop before the end of the array :

    for(int i=0 ; i < myArray.length - 1 ; i++){
        System.out.print(myArray[i]+",");
    }

Then, write the last element :

    System.out.print(myArray[myArray.length - 1]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.