This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Elements from an array into a grid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Elements from an array into a grid" Watch "Elements from an array into a grid" New topic
Author

Elements from an array into a grid

Chris Updike
Greenhorn

Joined: Apr 29, 2010
Posts: 2
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

Joined: Aug 03, 2009
Posts: 29

how about this one. Please try this and check if it works.


Dhan
SCJP - Here for Knowledge..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
That probably would work, but have you come across the System.out.println() call?
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

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

Joined: Aug 03, 2009
Posts: 29

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

Joined: Apr 29, 2010
Posts: 2
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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.
 
GeeCON Prague 2014
 
subject: Elements from an array into a grid