Hello,

My ouput is showing as...

"1 2 3   (10)(23)(29)"

when it should be...

"1(10)  2(23)  3(29)"

I don't think I am too far off. Here's the code and thanks in advance.

// int[] Groups = {10, 23, 29}; in the constructor

public String toString() 
   {
     String tempStringB = "";
     String tempStringA = " ";
     String tempStringC = " ";

      for (int x = 1; x<=3; x+=1)
      { 
         tempStringB = tempStringB + x + "  ";
      }    

       for(int i = 0; i < Group.length;i++)
       {
           tempStringA = tempStringA + "(" + Groups[i] + ")"; 
       }

       tempStringC = tempStringB + tempStringA;

       return tempStringC;
   }

Recommended Answers

All 4 Replies

int[] groups = {10, 23, 29};

public String toString()
{
    String buffer = "";
    for (int i=0; i < groups.length; ++i)
        buffer += i + "(" + groups[i] + ") ";
    return buffer.trim();
}

To help figure this out, you could have added some println() statements to show how the values of your variables change:

or (int x = 1; x<=3; x+=1)
{ 
tempStringB = tempStringB + x + " ";
System.out.println("tempB=" + tempStringB);  // show new B
} 

for(int i = 0; i < Group.length;i++)
{
tempStringA = tempStringA + "(" + Groups[i] + ")"; 
System.out.println("tempA=" + tempStringA);  // show new A
}
commented: 1 +1

public class NewClass {


public static int[] groups = {10, 23, 29};

public static void main(String[] args){

String s = "";

for (int i=0; i < groups.length; ++i)

s += i + "(" + groups + ") ";


System.out.println(s);
}
}

commented: 1 +1

thank you this has been solved.

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.