3 Replies - 575 Views - Last Post: 25 January 2013 - 10:53 AM Rate Topic: -----

#1 bradleycmetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Silly print logic question

Posted 24 January 2013 - 04:13 PM

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

This post has been edited by bradleycmetz: 24 January 2013 - 04:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Silly print logic question

#2 jjh08   User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Silly print logic question

Posted 24 January 2013 - 04:45 PM

I'm not sure based on the information provided, but did you place the comma in the print() method that is inside the for loop. Maybe that could be why it prints a comma after the nth number. Something like this:
for(int i =0; i <= n; i++)
{ 
  System.out.print(i + ",");
}


This post has been edited by jjh08: 24 January 2013 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Silly print logic question

Posted 24 January 2013 - 05:05 PM

You need to check if the current item is the last one that will be printed. That is, if the next item to be read will be beyond the end of the array.

// assuming step is the supplied step-value:
int size = myArray.length;
for (int i=step-1; i < size; i += step) {
    if ((i + step) >= size) {
        // if this is the last one..
        System.out.printf("%d\n", myArray[i]);
    } else {
        System.out.printf("%d, ", myArray[i]);
    }
}


BTW I haven't tested this code.

This post has been edited by andrewsw: 24 January 2013 - 05:12 PM

Was This Post Helpful? 1
  • +
  • -

#4 bradleycmetz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 17-September 12

Re: Silly print logic question

Posted 25 January 2013 - 10:53 AM

View Postandrewsw, on 24 January 2013 - 05:05 PM, said:

You need to check if the current item is the last one that will be printed. That is, if the next item to be read will be beyond the end of the array.

// assuming step is the supplied step-value:
int size = myArray.length;
for (int i=step-1; i < size; i += step) {
    if ((i + step) >= size) {
        // if this is the last one..
        System.out.printf("%d\n", myArray[i]);
    } else {
        System.out.printf("%d, ", myArray[i]);
    }
}


I came up with this after revisiting the problem this morning- a bit different albeit. Thanks as always for everyone's insight :)/>
BTW I haven't tested this code.

This post has been edited by bradleycmetz: 25 January 2013 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1