• Post Reply Bookmark Topic Watch Topic
  • New Topic

Times Table Output  RSS feed

 
Ja Shields
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

Forgive a possibly dumb question. I am trying to get the output to look like the attachment using actual math operations, converting integers to strings, formatting text, and nested loops. I am close (tell me I am), but can't quite figure out where I have goofed!

Here's what I have:



What's my dumb mistake that is causing it not to work like I want it to? Or is it worse than that?

Thanks!!
Jane
Times-Table.JPG
[Thumbnail for Times-Table.JPG]
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not bad for a start! You're not getting a grid because on line 34, getting the line separator isn't the same thing as outputting it. Something like System.out.print(System.getProperty("line.separator")); should work, but just System.println(); would be easier.

Second, you don't want to check if j < 10 in your loop, because your loop condition already ensures that. What you might want to do is leave line 30 in the inner loop, put line 34 after the inner loop, and do away with the if-else entirely.

And lastly, you've got your column headers thanks to line 18, but you don't have row headers. You'll have to figure out how print those too. Good luck!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your column header is fixed, instead of that incredibly ugly line 18, you could just do this:


or some equivalent, depending on your exact requirements...

Don't make things harder than they need to be.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to break your long lines; it makes the code much easier to read and you can see how you should do it. I also got rid of some of the blank lines because too much empty space ain't easy to read either.

Are you aware of the % tags? Find the Formatter class (difficult to read) and these two Java® Tutorials sections: 1 2 (easier to read). Rather than repeatedly adding spaces to your numbers, you can simply tell it to print the number as part of a 5‑character block. You can actually print all ten numbers across the screen like that with a single instruction, but remember you will need the %n tag too.
 
Ja Shields
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thank you so much for your help! I think I used all the advice and it is looking much better! I am so sorry for the ugly looking code previously. (I hope it looks much better now!!) I was using a format that was in a file our professor had sent us to do the column header and that is why it was so long and ugly. I have switched over based on the suggestions, but I am running into one problem. How do I keep it from doing a line break after every number? I did searching on the PrintStream class, but didn't seem to find anything that would help me. (Or maybe I didn't understand what I was finding. Quite possible.) Help! This is what I have now:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ja Shields wrote:How do I keep it from doing a line break after every number?

If your talking about the numbers in your header, then you need to take the %n out of your format call on line 16 and then add a System.out.println() after that loop.
 
Ja Shields
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! SO easy!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!