Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

how to return items from an array to be printed diagonal

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 12855
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 69373
276
  • Mark post as helpful
  • send pies
  • 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
 
We can walk to school together. And we can both read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic