0

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)

  • 1
    What do you mean "return the whole for loop" (and given that its a void method, it doesn't return anything...)? What is the expected output? –  Feb 20 '15 at 03:39

5 Answers5

1

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));

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
0

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

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();
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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.

Yogesh Patel
  • 672
  • 4
  • 9
0

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.

J Blaz
  • 783
  • 1
  • 6
  • 26
  • Do we need to discuss the issues of `String` concatenation in loops? – MadProgrammer Feb 20 '15 at 03:44
  • This is simple enough that it wont really matter. You suggested string builders and true that would be faster but its only 100 numbers. – J Blaz Feb 20 '15 at 03:45
  • 1
    Perfect practice makes perfect. Here's an opportunity to demonstrate good practices to an inexperienced developer BEFORE they develop bad habits – MadProgrammer Feb 20 '15 at 03:46
  • I hear that. But the simplest solution is the best solution. – J Blaz Feb 20 '15 at 03:47
  • I agree with @MadProgrammer that any developer should always try to do the best code either or not the snippet is simple. You don't want to start programming on bad base. – Jean-François Savard Feb 20 '15 at 03:48
  • That's a naive point of view, as doing `String` concatenation in a loop is NOT the best solution, but that comes to down to from which point-of-view you are taking to the meaning of "best" – MadProgrammer Feb 20 '15 at 03:49
  • @JaseBlaz it's not like using StringBuilder was harder/longer/painful... It's as simple as String, so why not use the proper method ? – Jean-François Savard Feb 20 '15 at 03:51
  • Taking a guess at the experience of the programmer a simple string would be the best solution. Giving the idea of a string builder will start a blind coding process where data structures are used without knowing why they are used. – J Blaz Feb 20 '15 at 03:51
  • @JaseBlaz so a String is not a "data structures" ? AFAIK it's an object as StringBuilder is. – Jean-François Savard Feb 20 '15 at 03:52
  • You might want to have a look at this http://stackoverflow.com/questions/4645020/when-to-use-stringbuilder-in-java – Jean-François Savard Feb 20 '15 at 03:55
  • Yes it is a data structure but since I assumed his level I also assume he knows how to use it. – J Blaz Feb 20 '15 at 03:59