-1

Hello I have an issue where I have to print out the numbers divisible by 17 but I want to print out 5 numbers and then jump down to the next line. This is the code that I have and don't know why it's not working....

public class seventeen {
   public static void main(String[] args) {
      int num = 17;
      System.out.print("The Numbers Divisible By 17 are: ");
      int enter = 0;
      for (int x = 1; x <= 10000; x++) {
         if (x % num == 0) {
            System.out.print(x + " ");
         }
         enter++;
         if (enter == 5) {
            enter = 0;
            System.out.println();
         }
      }
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
David
  • 173
  • 2
  • 5
  • 14

3 Answers3

6

You should only increment your counter when the number validation is reached. This means, move the enter++; inside the validation:

if (x % num == 0) {
    System.out.print(x + " ");
    enter++;
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Did you mean to make this one CW? – Dennis Meng Sep 05 '13 at 20:46
  • Yes, I think that this kind of answer shouldn't give me any rep. – Luiggi Mendoza Sep 05 '13 at 20:47
  • Wow thank you I'm sorry. The semester just started and I'm a little rusty.. Thanks! – David Sep 05 '13 at 20:49
  • @DavidCamacho you're welcome. Next time you have a problem, use a debugger to see why the code behaves *strange*. – Luiggi Mendoza Sep 05 '13 at 20:52
  • But the debugger didn't give me anything... it ran completely fine just with a lot of line breaks, I just wasn't thinking logically – David Sep 05 '13 at 21:00
  • @DavidCamacho using the degubber would have shown you that the `enter` variable increments on each iteration. Then you have wondered *why does my code behave like this?* and probably get to the conclusion the counter should be inside the validation. – Luiggi Mendoza Sep 05 '13 at 21:02
  • Now, I know this is WAY off topic, but my teacher didn't provide us with a book for data structure (this is my 3rd programming course) and we just have a simple online ebook. Which one do you recommend? The course is called COSC Data Structure in Java – David Sep 05 '13 at 21:02
  • @DavidCamacho hover the mouse over the `[java]` tag and select the [info](http://stackoverflow.com/tags/java/info) where you will access to StackOverflow's Java wiki. There are plenty online resources there from basic to intermediate-advanced topics to learn. – Luiggi Mendoza Sep 05 '13 at 21:05
  • I'd move the `if (enter == 5) { ... }` inside `if (x%num==0)` also. No point in testing it in every iteration; it only matters on the ones where you actually print something. Also, at the end of the program, make sure you don't leave in the middle of the line: `if (enter > 0) System.out.println();` – ajb Sep 05 '13 at 22:00
0

changing your loop to for(int x = 17 ; x <= 10000 ; x+=17) should solve it, and make your code about 17x faster.

Then you can remove your modulus check as well.

Cruncher
  • 7,641
  • 1
  • 31
  • 65
0

As others have posted, incrementing enter should be done only when you actually print something; and there's no point in testing it for 5 in the loop iterations when you don't print something.

Another point is to make sure you finish correctly. If the last line has 5 numbers, then enter will be 0 after the loop is done, and you'll be on the next line. But if the last line is short, you want to make sure to do a println() after you're done. So:

  int enter = 0;
  for (int x = 1; x <= 10000; x++) {
     if (x % num == 0) {
        System.out.print(x + " ");
        enter++;
        if (enter == 5) {
           enter = 0;
           System.out.println();
        }
     }
  }
  if (enter > 0)
     System.out.println();

(P.S. I'd actually use the x+=17 method in other answers, but this idiom could come up in many other cases where you actually do have to look at every item and only print some of them.)

ajb
  • 31,309
  • 3
  • 58
  • 84