This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

printing character array vertically with loop

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic