• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to return items from an array to be printed diagonal

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm working on a method that takes in an array called number and returns its contents to be printed in a diagonal like so:

with tabs in between and each element printed on a new line.
Here is my method which is obviously very wrong but its what I have.

Just cant figure out how to incorporate the new line into a loop or object for that matter.
Thanks,
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use "\n" for newline. Also use StringBuilder instead of + operator to join Strings in a loop.
 
Johnny Steele
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Sorry about posting this twice. i know to use "\n" but as of now I cannot use StringBuilder. I'm just not sure how or where to put "\n" in order to get the desired diagonal printed.
Thanks again for the response,
-john
 
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
part of your problem is that your inner loop declares "int j", but then you use write "i <" and "i++".

that's going to mess up your outer loop, and your inner loop probably won't do what you want.
 
Marshal
Posts: 79979
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually you ought not to use "\n", which is operating-system dependent. You ought to useMore details in System#getProperties().

I think you need some way of counting tabs, and put the \t character before the number in the String. Once yo have managed that, things will get easier. As for StringBuilder, it is easy to use
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic