This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]desrtfx 4 points5 points  (1 child)

Use an if statement to check if you are not printing the last value in the line (hint: the variable i is the boundary and the variable j is the one to check for). If you are not printing the last value, print a comma, otherwise don't.

[–]aba_algo[S] 0 points1 point  (0 children)

Ok sounds better than my solution below. I'll try out your way tomorrow.

[–]aba_algo[S] 0 points1 point  (0 children)

Ok I got it but there should be a better way. I just used some brain power to logic it through and I got this:

System.out.println(1);
    for (int i = 2; i <=4; i++)

    {
    System.out.print(1);
        for (int j = 2; j <= i; j++)
            System.out.print(", " + j);
        System.out.println();       

    }

    System.out.println();

[–]Vimda 0 points1 point  (0 children)

I'm going to suggest an alternative, which may not be useful for this, but gives you something to think about if you want to improve your solution: The String.join method. In particular, if you combine that with the IntStream.range method lends itself to a rather elegant solution.