-7

I have a simple but hard problem and wanted to get your help on this. This is the code:

int i = 0; 
while (i < 100) {
      i++; 
     System.out.print(i);
}

This is the real issue that I'm having, how do I control the println to display how many numbers per line that I want so I don't just see 100 numbers in a row straight? Btw please if at all possible, don't give me the answer but help me to answer it myself instead.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
Dumbfounded
  • 11
  • 1
  • 2
  • 6
  • Check this question: http://stackoverflow.com/questions/4008223/print-in-new-line-java – lleaff Jan 02 '16 at 00:32
  • 6
    `if (i % 10 == 0) { System.out.println(); }`? – MadProgrammer Jan 02 '16 at 00:35
  • 1
    Check the value of `i` and when you get to the number you want (you can use modulus operator to determine if i is at the count you want) just print a new line. – scrappedcola Jan 02 '16 at 00:35
  • The problem is that its going to print 100 in a row, i just wanted to split them into rows of 10 or whatever i wanted. i tried madprogrammer but it just gave me 102030405060708090 – Dumbfounded Jan 02 '16 at 00:41
  • 1
    @Dumbfounded because you need to add spaces too. Right now you're just printing each number right next to each other. Try `System.out.print(i + " ");` instead, with MadProgrammer's tip too. – Ricky Mutschlechner Jan 02 '16 at 00:46
  • Does this answer your question? [How to convert object array to string array in Java](https://stackoverflow.com/questions/1018750/how-to-convert-object-array-to-string-array-in-java) – 9ilsdx 9rvj 0lo Feb 09 '22 at 09:16

3 Answers3

2

while loops? Why not use for loops? They are much better in this kind of situation i.e. when you want to repeat something a known number of times.

You can use a nested for loop to make this happen:

int counter = 0;
for (int i = 0 ; i < 10 ; i++) {
    for (int j = 0; j < 10 ; j++) {
        System.out.print (counter);
        System.out.print (" "); // I think it is best to have spaces between the numbers
        counter++;
    }
    //after printing 10 numbers, go to a new line
    System.out.println ();
}
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • sweeper do i have to use 2 for loops just to make it print 10 per line? I just wanted it to print like a calender ie.. 1234567890, next line under number 1 it would be 11121314151617181920 then next line would be 21 all the way to 100, can i use 1 loop instead? – Dumbfounded Jan 02 '16 at 14:44
  • Yes you can! Just like MadProgrammer has said in the comments, you can use one for loop to print 1 to 100. Every iteration, you check if `i % 10 == 0` i.e. If `i` is divisible by 10. If it is, then go to a new line. @Dumbfounded – Sweeper Jan 02 '16 at 14:52
  • Thank you very much on this, it did try it but it didnt work at first but i was blind and couldnt see now I do. I tried looking on the net for just a straight answer to this but everybody had a huge problem but i just wanted 1 part of the problem so I asked here. now that I have it thanks to madprogrammer and sweepers help, here is the code for future reference, int i = 0; while (i < 10) { i++; if (i % any number == 0) System.out.println(); System.out.println(i); – Dumbfounded Jan 02 '16 at 15:22
  • If you think my answer helped you, please accept it by clicking on the tick ✅ on the left of the answer. – Sweeper Jan 03 '16 at 00:22
0

You could do something like:

    for(int number = 0; number <= 100; number++) {
        if(number % 10 == 0 && number > 0)
            System.out.println(number);
        else
            System.out.print(number + " ");
    }

This would create 10 rows of 10 numbers.

Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
Amit
  • 85
  • 5
0

Try this one

if (i%10==1){
     System.out.println("");
 }
yash
  • 1,357
  • 2
  • 23
  • 34
Java noob
  • 3
  • 2
  • Hey Firas, welcome to SO! When answering it helps to be a little more complete, so maybe edit your answer to show how this would work in context of the original problem. Also, it's good to add an explanation of *why* your solution works, especially in this case where they have asked for direction. Last tip, check out the formatting tools. adding three back-ticks in a row will start a code block, and three more will close it. – sammms Jan 14 '20 at 23:21