0

I am working with a program in which I need to use a method call to return a String from an int array. This is what I have of the method so far (I am required to use a method with this header and parameters)

    public String toString()
{
for (int i=0;i<NUM_POCKETS;i++)
    {
    this.getPocketCount(i);
    }
}

I basically need the loop to go through all of my "pockets" (array items) and return the stored values into a String to be returned.

I could be missing something very obvious, but for the life of me I do not understand how this information would be stored and returned to the Driver as a String. I know the loop logic is there, but how do I store each increment of i into a String as the loop progresses?

Thanks in advance for any help!

Brett
  • 1

4 Answers4

3

"I am working with a program in which I need to use a method call to return a String from an int array."

If this isn't a homework problem, you can simply use Arrays.toString(int[] array).

String myString = Arrays.toString(myIntArray);

Otherwise, maybe you can do something like this:

String getStringFromIntArray(int[] array) {
  StringBuilder builder = new StringBuilder();
  for (int num : array)
    builder.append(num)
  return builder.toString();
}
yamafontes
  • 5,552
  • 1
  • 18
  • 18
1

Try looking into the StringBuilder class. The specification for Java 6 is here.

http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html

You would need a StringBuilder object and just append the value to the object using the .append() function.

burgermenu
  • 191
  • 9
  • This would indeed be one of the most elegant solutions, next to `Arrays.toString(...)`. Especially if you want to add further information, this should be the solution of choice. – blalasaadri Oct 24 '13 at 21:54
1

as long as this.getPocketCount(i); gives you the value of the array on position i:

public String toString() {
    String returnstring= "";    //init empty string
    for (int i = 0; i < NUM_POCKETS; i++) {
        returnstring += this.getPocketCount(i)+" "; //append(concat) to string
    }
    returnstring = returnstring.substring(0, returnstring.length()-1);  //remove the last " "
    return returnstring;    //return string
}

the + sign appends the next string "Hello"+" "+"World" becomes "Hello World"

Edit:

public String toString() {
    String returnstring= "";    //init empty string
    for (int i = 0; i < NUM_POCKETS-1; i++) { //-1 to place last later
        returnstring += this.getPocketCount(i)+" "; //append to string
    }
    returnstring += this.getPocketCount(NUM_POCKETS-1)  //append the last value
    return returnstring;    //return string
}
YAMM
  • 572
  • 4
  • 9
  • If no outside tools are to be used, this would be a good solution. Maybe only add the space if you're not looking at the last element or something. – blalasaadri Oct 24 '13 at 21:59
  • 1
    this would cause many if statments... i will fix it in a better way – YAMM Oct 24 '13 at 22:01
0

Assuming you specifically want to build your String from within the loop, you could use a StringBuilder. It is more verbose than the one-liner offered by Arrays.toString(), but you asked for it:

e.g.

public String toString() {
    StringBuilder sb = new StringBuilder(NUM_POCKETS);
    for (int i = 0; i < NUM_POCKETS; i++) {
        sb.append(this.getPocketCount(i));
    }
    return sb.toString();
}

Using a StringBuilder within a loop is faster than performing concatenation of each individual element. See: when to use StringBuilder in java

Community
  • 1
  • 1
x4nd3r
  • 855
  • 1
  • 7
  • 20