• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Printing with 2D Arrays

 
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I would like to print this out using arrays and for loops:

Test     Student 1     Student 2     Student 3
Test 1      89                 76                88
Test 2      78                 87                70
Test 3      90                 94                20

But my program does not work! It outputs everything side by side, like this:
 


This is my program:




Thank you for your help!
 
Sheriff
Posts: 24753
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want your output to start on a new line, then



will do that for you.
 
Mona Smith
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Yes I forgot to println! Thanks for that. But it still outputs as:



Whereas I want it to be in three rows according to test #.
 
Mona Smith
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Thank you! Can you specify what you mean by that? Like this?



That gives me this output:



 
Marshal
Posts: 66575
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at your code formatting. Start by removing the comment in line 12. Change i in line 13 and everywhere else to row. Do the same with the included loop. It is usual to use single‑letter loop variables, but if you have array[row][column], it is easy to read and you no longer need any sort of comment. Add {} to the inside loop. You can now see where the loops start and finish. You can also see which code is inside the loop and which outside.
At least you have indented the code correctly so I could see that the last lot of code you posted doesn't print what you said it does. It actually prints

Test 1897688Test 2788770Test 3909420

Suggestions:-
  • 1: You are iterating both arrays forward, you are iterating the whole of each array, and you are not trying to reassign any of their elements. Use a for‑each loop (=enhanced for loop) instead of an ordinary for loop.
  • 2: Don't use \n. Use System.out.println() as Paul C has already told you.
  • 3: Find out about System.out.printf(). The simple details are in the Java™ Tutorials; you will find the full details here, but don't try to remember all those details just at the moment. Try System.out.printf("%8s", someText); and see what happens.
  • 4: Work out how many new lines you need and put println() in those places.
  •  
    Carey Brown
    Saloon Keeper
    Posts: 6462
    61
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Mona Smith wrote:OK Thank you! Can you specify what you mean by that? Like this?


    No, not like that. What you've done is print a new-line after the 'i' loop, not the 'k' loop. Enclose the body of the 'k' loop in braces and it may become a little clearer to you.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi, you can try it like this.

     
    Mona Smith
    Greenhorn
    Posts: 15
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello,

    Ok thank you everyone for your suggestions! The reason why I have extra comments is because my teacher requires us to do so. But this is my code using your suggestions:


     
    Campbell Ritchie
    Marshal
    Posts: 66575
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Mona Smith wrote:. . . I have extra comments is because my teacher requires us to do so.  . . .

    I think it is a good idea to use comments to explain anything obscure, but it is usually better to make the code clear so there is no obscurity. But if you are told to use comments, you had better do so.
    I still think you would get better results with printf than appending tab characters.
     
    Carey Brown
    Saloon Keeper
    Posts: 6462
    61
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I still think you would get better results with printf than appending tab characters.


    +1  Totally agree.

    printf() can get pretty fancy with some data types, but for Strings, it is dirt simple:
     
    Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!