aspose file tools*
The moose likes Beginning Java and the fly likes printing character array vertically with loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "printing character array vertically with loop" Watch "printing character array vertically with loop" New topic
Author

printing character array vertically with loop

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello, I have a tic tac toe board class, which will be used to instantiate board objects. I am kind of confused how to print out the character [] vertically. I have done this before with using loops. Does the same approach need to be taken? Is there any methods on java api that will flip characters in an array for you? The reason I ask is bc I am having problems implementing this loop into my board class code. It say board[i] can not be dereferenced. I already have a char[] with a reference to this array called board. I don't know if this class works properly bc I can't get it to display the following;
xox
oxo
***
something like that; so that the characters appear vertically. All I got with 3 system.out.print(board[i]) calls was the following ***
or 3 system.out.println(board[i]) calls was
*
*
*
I have made a program that took a string and did this before, can I use that class in this board class program?
well anyways I'll post my code for the board class. If anyone could please suggests something; i'll try anything. Thanks in advance.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Smith:
... It say board[ i ] can not be dereferenced. I already have a char[] with a reference to this array called board...

Hi Mike,

Basically, the dot (.) operator "dereferences" a variable that holds a reference to an object. You might think of it as "getting" the object. This allows you to access an object member -- for example, a method or field...

myObjectRef.myMethod();

But variables that hold primitive values cannot be dereferenced. There's no object to "get," and no "members" to access. That's the issue here: board is a char array, so char[x] represents a primitive char. Therefore, you can't dereference it with a call like char[x].nextChar().

(In your previous code, your array held object references instead of primitive values, so you could dereference with str[x].nextChar().)

Originally posted by Mike Smith:
... Is there any methods on java api that will flip characters in an array for you? ...

Not really. This is something you need to code.

For displaying the array, you basically need to print 3 chars per line. There are a variety of ways to do this, but here's a very straight-forward approach...

System.out.println("" + board[0] + board[1] + board[2]);
System.out.println("" + board[3] + board[4] + board[5]);
System.out.println("" + board[6] + board[7] + board[8]);

Note that there's no need to loop here, because the size of the board is pre-defined. Also note that I started each line with an empty String so that the + operator concatenates subsequent values as Strings, rather than adding them as values.
[ November 09, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello, thanks for the reply. That idea never occurred to me. I always have a tendency of thinking more about a problem and skipping the simple tasks and jump right into the more complex ways of handling a problem. I got the board to print out. But now I am faced with a problem of updating the array. Although, thanks for the input; it is greatly apprecited.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Smith:
...now I am faced with a problem of updating the array...

I suggest approaching that in a straight-forward way as well (literal assignments, like board[3] = 'x'; ). Keep it simple.
[ November 10, 2005: Message edited by: marc weber ]
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Thanks Marc, I finished the assigment. Everything works good except the winning move. I have to wrap my thinking into more simplier terms.Thanks for the input though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: printing character array vertically with loop
 
Similar Threads
Simple logic is kicking my butt.
synchronization.. and threads !!!
Converting Infix to Postfix Expressions
tic tac toe and switch statement
creating methods