[Logo]
Forums Register Login
Boolean Convert To Int Error
Hello Java Ranch,

I have been working on a TicTacToe console game in Java. I have a error and hope to learn more about java and how to fix this error. Boolean needs to be converted to Int. I have tried to add code to convert but got more errors.


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: boolean cannot be converted to int
at javaapplication46.tic.JavaApplication46Ticjava.main(JavaApplication46Ticjava.java:74)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
Can you put the code, to see how you are doing it
(1 like)
Some languages have implicit conversion from int to boolean or vice versa. Java doesn't.

You could use

 

Brian Bykenhaal wrote:I have been working on a TicTacToe console game in Java. I have a error and hope to learn more about java and how to fix this error. Boolean needs to be converted to Int. I have tried to add code to convert but got more errors.


Well, that's always a bad move. Never add code until you know what the problem is.

However, without seeing your code (and only the code that is causing the error, please), we can't help much. I'd care to bet, though, that you're assigning a boolean to an integer.

Winston
 

Marc Cracco wrote:Some languages have implicit conversion from int to boolean or vice versa. Java doesn't.

You could use



using 1 to true and 0 to false.
 

Ubaldo Robledo wrote:
using 1 to true and 0 to false.



Was that a question or were you agreeing?
 

Marc Cracco wrote:Was that a question or were you agreeing?


I think Ubaldo was clarifying. Think about it.

Winston
 

Marc Cracco wrote:
Was that a question or were you agreeing?



I was agreeing, cause sometimes code is confusing. (no this one) but im new and i understand how hard is sometime to catch some info.
 

Ubaldo Robledo wrote:

I was agreeing, cause sometimes code is confusing. (no this one) but im new and i understand how hard is sometime to catch some info.



Gotcha, well here is a little more information on the line of code I wrote, it's called a Ternary Operator, more specific checkout the Java Usage Section.
 

Brian Bykenhaal wrote:Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: boolean cannot be converted to int


The message "Uncompilable source code" suggests to me that not all your class files are up-to-date - you might be mixing *.class files from a previous version with other *.class files in which you changed things. Try re-compiling the whole project.
 

Brian Bykenhaal wrote: . . .

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - . . .

Welcome to the Ranch

You only ever get that sort of error message from an IDE; the rest of the message has a NetBeans‑y style. Many of us round here think it is not a good idea to start learning on an IDE, but if you have got used to your IDE, you should never try to run uncompiled code. There should be a red mark against the code where the compiler error occurred, and you might be able to get more information by hovering on the red mark with the mouse or clicking it.
Hey

Wow thanks for the information yeah I am pretty Green at Java and only messed with java scripts in Html. I want to make a Three in a row Game as you all know. I wanted to add something like the Tic Tac Toe named TTT Cosnole Non OO2P.java at this url webpage
The problem is I can now get the program to run. I do not know where to go with this code. It is more complex then the last really simple game I made from a YouTube channel a while ago.

Error with my code
I hit run yes in my net beans and I see my board and then I type any of the spaces. I used rows and columns in like battle ship style. So I type in 1 1 for row 1 column 1 and then I get a error like this


If you think it is a compile error I will see if I can compile but I never compiled this with Command prompt. So I have no class file with this code yet.


Don't use numbers to represent the different players. Use an enumerated type.
Couple of things off the bat.

winOrTie() can't return nothing. If you don't know what code will go in there for now either return true or false.


since outcome is an int you can give it a value from a boolean, for now I commented it out.
I don't know if I had my braces off or you but after cleaning that up I got the game to run.

Next I wouldn't throw an Exception if a slot is taken. Show messaging and repoll the user for a square to play.
Thanks for the help I got it to run and it understood the numbers I typed in. The problem seems to be the my rows and columns I put in 1 1 and got the middle space. I wanted 1 1 is the top left corner. Any Idea what went wrong?

System.out.println("This move at (" + (row + 1) + "," + (col + 1)
+ ") is not valid. Try again...");
}
} while (!validInput); // repeat until input is valid
}
Nothing went wrong; the top left is (0, 0), rather than (1, 1).
To make it more user friendly you could take the inputs and remove 1 to make it match the array index which start at 0.

For the enum talked about you could use



Also consider having the main do while loop check a boolean called gameComplete or something like that. Near the end of the loop call a method (i.e. checkGameStatus()) that could check to see if someone one, and or if the game is tied. If the game is finished update the gameComplete variable to true and set a variable called winner with the proper player.

These are just ideas on how I would do it.
I had it working a little then I messed it up and and saved it. This is what I have now.

Thanks for the help like I said I got the program to run. I did what you told me and put in 1 1. I then got the middle of the board. I tried to fix it and saved it to run it with the changes and ended up wishing I never changed after I got it to work. If you can tell me from what I have on my last code post what is it that I am doing wrong I will run it and save. I like the enum idea I watch a youtube video and the programmer used enum.
Take a look below, still a lot missing but hopefully it helps.

Thanks the java works great! I still have some issues like when I put in a wrong number. I would like to no have a error. I have seen code like below used. Maybe a else statment.


System.out.println("This move at (" + (row + 1) + "," + (col + 1)
+ ") is not valid. Try again...");
}
} while (!validInput); // repeat until input is valid
}
 

Brian Bykenhaal wrote:Thanks the java works great! I still have some issues like when I put in a wrong number. I would like to no have a error. I have seen code like below used. Maybe a else statment.


System.out.println("This move at (" + (row + 1) + "," + (col + 1)
+ ") is not valid. Try again...");
}
} while (!validInput); // repeat until input is valid
}



I'm not sure I understand what you want to do. Do you want it to show an error or not? Right now it does and queries the user again. What would you want the flow to be?

Have you figured out the logic for finding a winner yet?
Have you considered creating this over multiple classes like mentioned in the commnents? It's a little more complex but makes more sense. You could create a TicTacToe class. The main method would then create an instance of that class and then when the game is finished it could either quit or let the user create a new one. It would also stop having those static method which isn't the best way to do this.
Hey thanks

I am working on either adding a scanner like you said or if statements to check to see if I have a winner. The static is fine for now I will be remaking this game with modified code from this version in the near future. For now I think I will be staying true to the very simple version of the console game. Like on the like given. I have updated code below but first is some code I was messing with. The project is coming along nicely. Once I learn how to find a winner I should be happy with the code. Like you said a Scanner would work best for checking to see if there is a winner. Below is done with a if statement.











 

Brian Bykenhaal wrote: . . .
// The whole game should be wrapped in a game object . . .

No, the whole game should be a game object. You should design your entire code with that in mind. You should have objects for the game, the board, the squares, and the players. If not more.

Your design with all those static keywords is inconsistent with that aim, I am afraid.
This is an example of the a game I would like to make the only difference is I would like to use the code I have already created. I like that this version works really well try it out. Thanks I will keep that in mind and in the future I will have Tic Tac Toe game with different classes. Thanks again.




Hey

I am back working on my Java game, I seem to be lost to what I am missing for the game to Scan for a winner. As someone posted earlier Scanner could scan to find a winner. This sounds great the way I have it now I am using checkGameStatus(). I understand a while loop is better then hard coding the entire win part of the game.
Why would you use scanner to scan for a winner? You need to write some logic that's going to look at the board.

There needs to be some logic as to what to check and in what order.
  • 1) Is there a winner?
  • 2) Is there more moves available?


  • For one you need to figure out what is considered a move and write code to check for it.
    If one is false then go to two which can just check if there are any empty slots left.
    This is what I am working on after a Youtube channel I used boolean flags for logic. This seems to be the easiest way to check for a winner because I do not need to hard code everything. I plan on implementing this code below into my game so that a winner can be found. Thanks again for scanner I was told that I could use a scanner to check for a winner but will not use the scanner to check for a winner. Thanks again.


     

    Brian Bykenhaal wrote: . . . Scanner could scan to find a winner. . . .

    That seems unlikely, as Marc Gracco has already said.
    Why are you using all these different flags. It's not like you're telling the winner "You want by going across" so why not use 1 flag?
    I have tried using a char[][] array as well to store the 'x' and 'O', and flags but still its not working all the time. I will work on cutting down the flags to just one. To check the change in the board. Then to find a winner thanks for the help.
    Are you using an IDE or coding in a text editor, if you're using a text editor see below....

    Back in the days when I was using a text editor I got used to using a lot of system.out for debugging. A nice clean way I use to do this is as follow.



    You can use that if statement all over your code and leave it there. When you're debugging a problem turn the boolean to true, the rest of the time leave it as false.
    Hey

    Great I have Net Beans and notepad++ I am in Net Beans thanks I have my Boolean set to true for debugging. I have been using the features with NetBean to help clean up my code I like this feature a lot. I have logic this is my hardcoded version. As you said I had to many flags so I will go with something easier.
    "I hope"




    you're returning a string yet you're return statement does not always



    You also have two differnet variables, one is a board and one is a gameboard. Copying code from elsewhere is not going to work if you don't look at your variables. By copying you also ended with tons of extra }

    You also have empty methods like checkRow and such which seem redundant since you also have getWinner()
    Thanks for the crash course I plan to take your advice and have started again one the Java Tic Tac Toe. I have it all planned out this time around. This way I can stay to one path like using a array to check for a winner and a try catch black to catch exeptions to my set method.
     

    Marc Cracco wrote: . . .
    . . .

    Not convinced. There is something wrong with a method which has to return null. Also that appears inappropriate use of strings. You should have a Player class, which will obviously have two instances O and X. You should only call any winner's method when the game is finished and you have not got a tie.s
    Hey I made some logic for my game I try both ways and the if statement seams right. I think I need to prompt the game to stop when I have a winner. I do not know how to do this yet but I can share what I have done already. I was going to remake the program and ran into trouble. I am working on making some logic to check for winners and to see if it is a tie. I was able to research this and found that two ways are most popular the 0-9 method and the [1] [1] to [3] [3] I like the second because I have been using that method all along. I was planning a lot of brute force coding in the form of if statements. What do you think am I on the right track? Thanks again


    Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


    This thread has been viewed 5157 times.

    All times above are in ranch (not your local) time.
    The current ranch time is
    Jan 18, 2018 05:18:38.