• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Number Grid

 
Greenhorn
Posts: 5
C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys

I wonder if anybody can help me with this little problem that I am having with a Java app.
I would like to display a kind of grid of numbers starting from 0 to 100 and with 10 rows, each row showing 10 numbers, so the first row would have 0 to 10 and the second would show 11 to 20 etc.
Example output:

0 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20

I have this so far but I am so lost

public class Main {

public static void main(String []args) {

for(int x=0; x<9; x++) {
for(int i=0; i<=10;i++) {
System.out.println(i + x);
}
System.out.println();
}
}
}

Any help would be greatly appreciated guys!
This is not any class task or assignment, I'm just interested to see how it's done:)
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what is wrong with this code? Does it compile? Does it run? throw an error?

It helps us tremendously if you TellTheDetails.

Here is a hint though...

What does "System.out.println" do that "System.out.print" doesn't?

UPDATE:
I looked a little closer...and I think there is more wrong here than I initially thought...

perhaps you should go back and think through what you want to do. Programming is really 90% thinking, and only 10% typing. So tell us in English what you want to happen. Your example is confusing as well. You say you want to print 10 numbers per row, but your example has ELEVEN numbers on the first line...
 
Alexis Panopoulos
Greenhorn
Posts: 5
C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thank you, I thought about it some more and finally got it to work, final code:

public class hi {

public static void main(String[]args) {
int[] row = new int[11];

for(int i=0; i<=9;i++) {
for(int j=1; j<=10;j++) {
row[j] = (10*i)+j;
System.out.print(row[j] + " ");

}
System.out.println();
}

}

}


Output:


1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
 
Marshal
Posts: 80647
473
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done

Now some challenges for improvement.
  • 1: Print it out with equal spacing, so 10 in the top row is exactly above 100 in the bottom row.
  • 2: Work out how to use only one print statement.
  • Hints:
  • 1: Finding out about System.out.printf and the % tags will help.
  • 2: I would probably use the String#format method. If you can work out the % tags for printf, they work exactly the same way for String#format.
  • There is actually an advantage to using few print calls. Each print call takes a certain time, maybe a few milliseconds. That doesn't sound a lot, but if you are unlucky your 110 print instructions might take a half‑second or longer. Creating a String object probably takes a few microseconds or less. You can save time with a StringBuilder.

    If you need a line end, try this, and don't use \n
    private static final String LINE_END = System.getProperty("line.separator");
    If that doesn't work I have got a spelling error in that
    You can read about the % tags in the Java® Tutorials in two places: 1 2. One of them sorts out the line end problem.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic