Keenan Kaufman
Keenan Kaufman

Reputation: 55

How do I return the string in this for loop?

I have this DieRolling class that we are making for AP Computer Science. This method is supposed to return all the numbers they have rolled in a "neat" manner. Here is the code:

public void printNum()
{
    for (int i=0;i<100;i++)
    {
        System.out.println("Roll " + (i+ 1) + ": " + numbers[i]);
    }
}

I need to return the whole for loop, but I cant figure out how. What should I do? Thanks!

(This is my first post on here so sorry if it is kind of messed up)

Upvotes: 0

Views: 74

Answers (5)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

You should declare printNum as a String then result a concatenate of all strings.

public String printNum(final int pToPrint)
{
    StringBuilder result = new StringBuilder();

    for(int i = 0; i < pToPrint; i++)
       result.append("Roll " + (i+ 1) + ": " + numbers[i] + System.lineSeparator());

   return result.toString();
}

Then you would call for example System.out.println(printNum(100));

Upvotes: 1

J Blaz
J Blaz

Reputation: 783

Just create a String variable String result = ""; and at each loop add numbers to the string. result += "Roll " + (i+ 1) + ": " + numbers[i] + "\n";

Or something. But you for sure need to change your return type. For the above solution it should be String instead of void.

Upvotes: 0

Yogesh Patel
Yogesh Patel

Reputation: 712

You can return only one time from a function. So if you want to return all numbers, you can return an array if that numbers.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

You could simply return the array...

public int[] printNum() {
    // ...
    return numbers;
}

If you want to format the result, you could use

public String printNum() {
    // ...
    Arrays.toString(numbers);
}

If you want to customise the format, you could use...

public String printNum() {
    StringBuilder sb = new StringBuilder(128);
    for (int i=0;i<numbers.length;i++)
    {
        System.out.println("Roll " + (i+ 1) + ": " + numbers[i]);
        if (sb.length() > 0) {
            sb.append(", ");
        }
        sb.append(numbers[i]);
    }        
    return sb.toString();
}

Or if you're using Java 8

public String printNum() {
    StringJoiner joiner = new StringJoiner(", ");
    for (int i=0;i<numbers.length;i++)
    {
        System.out.println("Roll " + (i+ 1) + ": " + numbers[i]);
        joiner.add(Integer.toString(numbers[i]));
    }        
    return sb.toString();
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You'll first need to change the return type from void to something reasonable. I suggest that you use either an array of int, since you apparently know that you'll be returning exactly 100 numbers, or an ArrayList<Integer> if that assumption is incorrect, fill up your array or List in the loop, and return it. AGain, you'll need to change the return type to be the type of whatever you decide to return.

Since this is homework, the details should be left to you.

Upvotes: 0

Related Questions