-5

Im trying to make a table that prints numbers from 0 to 255 in decimal format. The print out should be 16 numbers per line using printf and %5d format. I made a code but idk how to make it enter after every 16 numbers.

    for(int x = 0; x <255;x++) {
         
    System.out.printf("%5d\n",x);

But it prints out in a line lol.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
bunny
  • 1
  • 1
  • 2
    Java is to Javascript as Pain is to Painting, or Ham is to Hamster. They are completely different. It is highly recommended that aspiring coders try to learn the name of the language they're attempting to write code in. When you post a question, please tag it appropriately - this lets those with knowledge of the language you need help with to see your question. – CertainPerformance Sep 09 '20 at 15:48
  • Does this answer your question? [Java String new line](https://stackoverflow.com/questions/7833689/java-string-new-line) – code11 Sep 09 '20 at 15:51
  • [`for(int i = 0; i < 16; i++) { for(int j = i * 16, l = (i+1) * 16; j < l; j++) { System.out.printf("%5d", j);} System.out.println();}`](https://ideone.com/6UqLlA)? – Lino Sep 09 '20 at 15:52

2 Answers2

1

you can add a simpel condition

 for(int x = 0,y=0; x <255;x++,y++){
            if(y==16)
            {
                System.out.printf("\n");
                y=0;
            }   
            System.out.printf("%5d",x);
                
        }

so what's happening here is that i added y veriable that's increments, every time y is equal to 16 it's entering the if condition,there we printing a new line and to make it so every 16 numbers the program will print a new line im returning y to 0.

tnuva
  • 35
  • 5
  • Thank you so much! Can you explain what you did though by adding the y=0? How does it know to make 16 numbers per line? – bunny Sep 09 '20 at 16:07
0

try to add if statement:

if (i % 17 == 0) {System.out.Println("");

basically, every time the index equals a number that is divided by 17, it goes to the next line. And you may want to get rid of "\n" since it skips to the next line in every iteration.