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!

3
  • It's more than concatenation, there's also the formatting of an int to a string. Commented Oct 24, 2013 at 21:43
  • Looking for something like this(stackoverflow.com/questions/10904911/…) ?? Commented Oct 24, 2013 at 21:47
  • Arrays.toString(arr).replaceAll("[, \[\]]","") Commented Oct 24, 2013 at 21:49

4 Answers 4

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

This would be the "Java" way to do it. OP may be looking for the "learn about general programming" way.
Well, he didn't say it was homework or anything, so i assume he's allowed to do this.
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.

1 Comment

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.
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
}

2 Comments

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.
this would cause many if statments... i will fix it in a better way
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.