Ive been coding practice problems and troubleshooting them for many hours now- and I have a silly stumper that I cannot seem to overcome. I have a very simple code that prints gets a number of integers from the scanner, proceeds to scan that many integers from a user, then scans a final number n. You are then to print each nth number separated by a comma and a space. While this is all fine and dandy- print from the array with for loops incremented by n, I am infuriated by not being able to place my comma and println(""); correctly. I do not want a comma after the last nth number in the array so I came up with this
numnums= number of numbers
i = space in array
but this is not valid for many inputs...
This is not exactly necessary for credit, I would just like to understand the logic behind what I would like to implement D:
Of course after dinner and a glass of wine, it will come to me.
Thanks, as always for your help!
/e to give an idea:
input:
4
1 -1 01
2
output:-1, 1
input
7
1 2 3 4 5 6 7
2
output: 2, 4, 6
I just cannot seem to omit the comma on the last number, and add the new line.
numnums= number of numbers
i = space in array
if (i == (numnums) || i == (numnums-1) )
System.out.println("");
else
System.out.printf(", ");
but this is not valid for many inputs...
This is not exactly necessary for credit, I would just like to understand the logic behind what I would like to implement D:
Of course after dinner and a glass of wine, it will come to me.
Thanks, as always for your help!
/e to give an idea:
input:
4
1 -1 01
2
output:-1, 1
input
7
1 2 3 4 5 6 7
2
output: 2, 4, 6
I just cannot seem to omit the comma on the last number, and add the new line.