Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Elements from an array into a grid

 
Chris Updike
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. I looked for an introduction forum but didn't see one so I suppose this will be my intro.

I bought the book Head First Java, 2nd Edition about a week ago to give a go at finally learning a whole language in depth. To this point I've only done a bit of Perl and with that only enough to do some basic scripting. I have given Ruby a try about a year ago but got distracted and put it down. I must say so far this book has been excellent. Kathy Sierra and Bert Bates should be commended for being thorough and descriptive without being dry, which I think is an all too common programming book treatment.

I'm through chapter 5 and had a bit of brainstorming for my first independent programming project. With what I've learned so far I'm attempting to make a connect4 style game. So far I have made myself an array for the game field and am trying to print it to screen. What I'm looking for is output like so:

|1|2|3|4|5|6|7|
_______________
|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|
|-|-|-|-|-|-|-|
_______________


I'm doing so with a method to print the array to a grid with the follwing code:




My output unfortunately looks like so:

|1|2|3|4|5|6|7|
_______________
|-|
-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|
_______________


I understand that I need to find the last element in the row and insert a newline but am unsure where in my code this should be and how to find that end of row. Do I do with a modulus of the end element? Would that work with the element counter being an int?
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about this one. Please try this and check if it works.
 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That probably would work, but have you come across the System.out.println() call?
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That probably would work, but have you come across the System.out.println() call?


If the YOU was for me , its a System.out.println() call with empty string.
I believe both the following are same to get a new line to print.
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aha.. I got it.
You meant System.out.println() with no input at all.
Thanks Campbell. I had never used it like this. It tried just now and it works fine.

 
Chris Updike
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh man I can't believe I didn't see that. Thanks guys for pointing that out. I feel noob. Also, looking through that again had me realize I had my array switched around so instead of 6 rows and 7 columns I was getting 7 rows and 6 columns. That would have made connect 4 a bit difficult. Anyway, here's my resulting code for both the game field creation and the game field print. My next task is setting up the player classes and having them assign values to the game spots.


 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhan Kumar wrote: . . . Thanks Campbell. I had never used it like this. It tried just now and it works fine.

You're welcome. But the details are all there in the API documentation. You need to learn your way round that documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic