• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Issue with Simple Tic Tac Toe  RSS feed

 
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

For my Java homework this week, I need to create a simple char 2D array that resembles Tic Tac Toe. This is what I have so far



The issue I'm running into is that it's telling me with Line 20 that it needs a  Class or interface so I'm a bit lost on what to do. Any help is appreciated
 
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the formatting: there are hidden }s on lines 10 and 16.  Coding: }} can visually hide the 2nd }
Make sure there is a pairing } for every {
And that there are no missing or extra }s
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I should delete those extra {?
 
Norm Radder
Master Rancher
Posts: 3276
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

delete those extra {


Before changing the code, find the } that pairs with each {
If there are any extras they could be deleted.

The }} should be changed so they are on separate lines and easier to see.  Also the } should be in line beneath the line with the pairing {
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite understanding

The extra } on lines 10 and 16 seems to pair with the { on lines 7 and 13. Is that not the way it should be?
 
Sheriff
Posts: 12348
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First format the code properly so that closing braces are aligned to their matching opening braces. Then you'll see that the brace on line 20 is the extra unmatched brace.
 
Junilu Lacar
Sheriff
Posts: 12348
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code formatted properly:
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome. Thank you for the reply. Now I have a new problem. I try to run the program and I get the following errors

Error3, 43) java: package jdk.nashorn.internal.objects does not exist

Error3, 1) java: static import only from classes and interfaces

Error15, 9) java: cannot find symbol
  symbol:   method print(char[][])
  location: class com.magishine.TicTacToes

Error23, 9) java: cannot find symbol
  symbol:   method print(char[][])
  location: class com.magishine.TicTacToes

....Um...Help?
 
Junilu Lacar
Sheriff
Posts: 12348
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to import that package anyway? You shouldn't program to anything that's in the internal package.  If you just want to print out the array, use java.util.Arrays instead.
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Why are you trying to import that package anyway? You shouldn't program to anything that's in the internal package.  If you just want to print out the array, use java.util.Arrays instead.



it was a suggestion from InteliJ

Anyway I figured that out but I now have a different issue. This is my new code



However, my output looks like this

First Tic Tac Toe:
Second Tic Tac Toe:
[[C@2ac1fdc4[[C@5f150435

Where did I go wrong?
 
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sean,

you did not go wrong. In java, arrays (whether 1 or more dimensional) have not overridden the 'toString' method. When you print such an array as you are doing in the code, you get the outcome as defined in the 'toString' method from the super class (Object).

There is no alternative than to define a print method for your 2D arrays. Any idea how that could be done?

Other tip: why not define two static variables in your class, say:

and use these in you gamex definitions, it would make reading and typing a little easier.
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good news. I am very close to figuring the whole thing out



I just need to figure out one thing. My Array now looks like this

[[X, X, O], [O, X, X], [O, O, X]][[O, O, X], [X, O, X], [O, X, X]]

I want them stacked on top of each other without those brackets. What do I need to do?
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Good thing is I've figured out how to get them to stack....bad news is it looks really bad



Now it looks like

First Tic Tac Toe:
Second Tic Tac Toe:
[[
, X, X, O], [
, O, X, X], [
, O, O, X]]
[[
, O, O, X], [
, X, O, X], [
, O, X, X]]

How do I get it to look cleaner and without those brackets? I've tried (array).replace and it gets rid of the commas and that worked but when I try to get rid of the brackets it makes my code not work
 
Piet Souris
Rancher
Posts: 2835
96
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost there!

Let's start with the printout of a 1D array. You already discovered Arrays.toString(array), that would be perfect here. So

Now, a 2D array in java is in fact an array of 1D arrays (and a 3D array is an array of 2D arrays, ad infinitum or untill memory is full). So, this is possible:

There are better and more general ways to get it done, but this should be a nice start. The '\n' is no longer needed.

Edit: this way does not get rid of the square brackets, since these are part of 'Arrays.toString'. It is certainly possible to get rid of these brackets, but that would involve slightly more code.
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be System.out.println(Arrays.toString(array)) ?

What would be required to get rid of the brackets?
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, that would be fine, but what I had in mind was to issue:

To get rid of the brackets, we have to revise our print1DArray method. For instance

Adjust the 2D-print method accordingly.

Have you been told about generics and java 8 steams yet?

 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unaware of generics and streams
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Doesn't matter for now.
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. I had to step away from my code for a moment as I was getting too frustrated but I'll implement what you suggested and report back when I can. Thank you for your help
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is what I came up with for printing the 2d array



I'm getting an error with line 3 saying ; is expected. What am I doing wrong?
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, because it is not quite what it should be. Try this:

Note: if you don't like 'private' use 'public'.
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so this is what I've ultimately ended up with



But I can't use a method that doesn't return anything. What did I miss?
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These two lines:

Since the method 'print2DArray' already prints the array, it returns nothing ('void'). And so you cannot use it as argument for the 'System.out.print' method. But then, since print2DArray does alrady do the printout, we don't need the 'System.out.print' at all. So, simplify the two lines to this:
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such a rookie mistake lol. Ok fixed that but now my output looks funky. It says

X, [C@5f150435, [C@5f150435
O, [C@1c53fd30, [C@1c53fd30
O, [C@50cbc42f, [C@50cbc42f
O, [C@75412c2f, [C@75412c2f
X, [C@282ba1e, [C@282ba1e
O, [C@13b6d03, [C@13b6d03

Why is it doing that?
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
     (Forgive me the grin)

Compare your line 28 to my line 4. Do you see why, instead of the second and third character of the array being printed, the 'toString' version of the array itself is printed?
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, my god, do I feel dumb. I got it figured out now. Thank you so much for being patient with me and helping me with this. You've been a life saver
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, and don't worry: when I started, I often went through similar misery myself, and I bet it goes for all of us. It is simply part of the 'growing up'.   
 
Sean Mcintosh
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do have one final question. How does your print1DArray method work? Like, I got it to work ok but I'd like to know how it works so I don't make the same mistake again
 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you have a 1D array, say int[] array = {1, 2,3, 4}, then you can loop through the array in several ways.

1) the classical way is to use an index (sometimes still necessary)

2) the so called enhanced for-loop was an improvement, since we dont need the index anymore:

3) The java 8 Stream way, in this case:


But that is something for later

I used method 2). The problem is geting that comma between the chars, but not before the first character and not after the last character. There are several ways to deal with that problem, and what I did is just one of the possibilities. Maybe you can think of other ways to get these comma's in the correct places?

After all characters have been printed, I do a last 'System.out.println()', to force output that comes after this, (if any), to print on the next line, and not directly behind the array.

 
Junilu Lacar
Sheriff
Posts: 12348
201
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get everything working, take some time to refactor your code.

When you're just trying to make it work, your main focus is implementation detail so you use names like print1Darray. That's not a good semantic name though because it has nothing to do with the game of TicTacToe. Better names would be printBoard and printRow.

Your high-level code should tell a story of what the program is doing. Leave implementation details out of that story. Implementation details should be hidden away in private methods so that it's easy to change them without affecting anything in the higher levels of the code.
 
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small suggestion:



That's a bit wordy. The following is shorter and does the same thing:



It's also faster, but you'd only care about that if you were desperate to save every possible nanosecond. More to the point, I find it easier to read, although others might disagree.

 
Piet Souris
Rancher
Posts: 2835
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My intention with 'if (firstchar) ...' was to give an implicit example of the construct 'if (b) ...' and to avoid the use of 'if (b == true) ...'.
 
Paul Clapham
Sheriff
Posts: 23714
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also a good example of the rule "If you don't put brackets around the code controlled by if, then put that code on the same line as the if".

Which you well know: if you wrote



it's easy for the unwary maintainer to add another line after that which appears to be controlled by the if, but actually isn't. Like this:



Incrementing the count will happen whether or not firstchar is true.

(I still have code from the old days when I didn't follow that rule, and I still occasionally get bitten when I forget to add the brackets along with another line of code.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!