Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1

    Help Me With Boolean Errors?


    Hello,

    I have a problem in Java I need to convert Boolean to Int and all the ways I have tried have given me 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)
    Last edited by Bykenhaal; December 17th, 2013 at 01:03 PM.
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Can you show some source code? There are several reasons that this error may occur. If you truely need to convert a boolean to an int you'll need to do something like:

    java Code:
    boolean boolVal = true;
    int intVal;
    if( boolVal )
        intVal = 1;
    else
        intVal = 0;


    There are simpler ways to do this but that is the concept. Unlike C and C++ a 1 (one) is not implicitly a true boolean in Java.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    Originally Posted by stdunbar
    Can you show some source code? There are several reasons that this error may occur. If you truely need to convert a boolean to an int you'll need to do something like:

    java Code:
    boolean boolVal = true;
    int intVal;
    if( boolVal )
        intVal = 1;
    else
        intVal = 0;


    There are simpler ways to do this but that is the concept. Unlike C and C++ a 1 (one) is not implicitly a true boolean in Java.

    Code:
    package javaapplication46.tic;
    
    import java.util.Scanner;
    
    public class JavaApplication46Ticjava {
    
        public static int[][] gameboard;
        public static final int EMPTY = 0;
        public static final int NOUGHT = -1;
        public static final int CROSS = 1;
        
        static void set(int val, int row, int col) throws
                IllegalArgumentException {
            if (gameboard[row][col] == EMPTY) gameboard[row][col] = val;
            else throw new IllegalArgumentException("Player already there!");
            
        }
        
        static void displayBoard() {
            for( int r = 0; r < gameboard.length; r++) {
                System.out.print("|");
                for (int c = 0; c < gameboard[r].length; c++) {
                    switch(gameboard[r][c]) {
                        case NOUGHT:
                            System.out.print("O");
                            break;
                        case CROSS:
                            System.out.print("X");
                            break;
                        default:        //EMPTY
                            System.out.print(" ");
                    }
                    System.out.print("|");
                }
                System.out.println("\n-------\n");
            }
        }
        
        static void createBoard(int row, int col) {
            gameboard = new int[row][col];
        }
        
       
        
        static boolean winOrTie() {
        
        
            
        
            //Determine whether X or O won or there is a tie    
            return;
    
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            
            createBoard(3,3);
            int turn = 0;
            int playerVal;
            int outcome = -2;
            boolean b = true;
     
            do {
                displayBoard();
                playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
                if (playerVal == NOUGHT) System.out.println("\n-O's turn-");
                else System.out.println("\n-X's turn-");
                System.out.print("Enter row and column:");
                try {
                        set(playerVal, scan.nextInt(), scan.nextInt());
            } catch (IllegalArgumentException ex) {System.err.println(ex);}
            turn ++;
            outcome = winOrTie();
            } while ( outcome == -2 );
            displayBoard();
            switch (outcome) {
                case NOUGHT:
                    System.out.println("O wins!");
                    break;
                case CROSS:
                    System.out.println("X wins!");
                    break;
                case EMPTY:
                    System.out.println("Tie");
                    break;
            }
        }
        
    }
  6. #4
  7. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    And where is the error happening?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    The error happens when I try to play the game.
    I type in 1 1 enter

    for row 1 column 1

    I then get a error message.

    run:
    | | | |
    -------

    | | | |
    -------

    | | | |
    -------


    -O's turn-
    Enter row and column:1 1
    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:76)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)


    So I want to convert to convert Boolean to Int.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I want to convert to convert Boolean to Int.
    What value do you what to get for true and what value for false?
    See post#2
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    Originally Posted by NormR
    What value do you what to get for true and what value for false?
    See post#2
    public static final int EMPTY = 0;
    public static final int NOUGHT = -1;
    public static final int CROSS = 1;

    So I would like to have it tell who won either noughts or cross. I have been looking into Boolean and like what they did on this game.

    TTTCosnoleNonOO2P.java
    http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html

    I would like to implement it to mine own code.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Did you try the technique shown in post#2?

    I didn't understand if you answered my question in post#6 or not:
    What value do you what to get for true and what value for false?

    What answer would the winOrTie() method return? What would true mean? A win or a tie? Does that imply it is the end of the game?
    Last edited by NormR; December 18th, 2013 at 10:09 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    Originally Posted by NormR
    Did you try the technique shown in post#2?

    I didn't understand if you answered my question in post#6 or not:
    What value do you what to get for true and what value for false?

    What answer would the winOrTie() method return? What would true mean? A win or a tie? Does that imply it is the end of the game?
    I have tried technique from post 2 but did not get it running.

    Return NOUGHT if nought wins, CROSS
    if cross wins, 0 if there’s a tie, and another value (like –2, for
    example) if there are empty cells on the board.

    static boolean winOrTie() is to determine whether X or 0 won or there is a tie.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    This is my board

    createBoard(3,3);
    int turn = 0;
    int playerVal;
    int outcome;
    java.util.Scanner scan = new java.util.Scanner(System.in);
    do {
    displayBoard();
    playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
    if (playerVal == NOUGHT) System.out.println(“\n—O’s turn—”);
    else System.out.println(“\n—X’s turn—”);
    System.out.print(“Enter row and column:”);
    try {
    set(playerVal, scan.nextInt(), scan.nextInt());
    } catch (Exception ex) {System.err.println(ex);}
    turn ++;
    outcome = winOrTie();
    } while ( outcome == -2 );
    displayBoard();
    switch (outcome) {
    case NOUGHT:
    System.out.println(“O wins!”);
    break;
    case CROSS:
    System.out.println(“X wins!”);
    break;
    case 0:
    System.out.println(“Tie.”);
    break;
    }
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    another value (like –2,
    Make that value another final int instead of coding -2, use a variable:
    Code:
     final int NO_WINNER = -2;
    winOrTie() needs to return an int, not a boolean if you want to use the value returned in a switch statement with more than 2 cases.
    Last edited by NormR; December 18th, 2013 at 11:46 AM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    Here is my new improved code. I need to add a random number catcher and a scanner to see if someone has won. I man still learning about them. Thanks

    Code:
    package javaapplication46.tic;  
      
    import java.util.Scanner;  
      
    public class JavaApplication46Ticjava {  
      
        public static int[][] gameboard;  
        public static final int EMPTY = 0;  
        public static final int NOUGHT = -1;  
        public static final int CROSS = 1;  
          
        static void set(int val, int row, int col) throws  
                IllegalArgumentException {  
            if (gameboard[row][col] == EMPTY) gameboard[row][col] = val;  
            else throw new IllegalArgumentException("Player already there!");  
              
        }  
          
        static void displayBoard() {  
            for( int r = 0; r < gameboard.length; r++) {  
                System.out.print("|");  
                for (int c = 0; c < gameboard[r].length; c++) {  
                    switch(gameboard[r][c]) {  
                        case NOUGHT:  
                            System.out.print("O");  
                            break;  
                        case CROSS:  
                            System.out.print("X");  
                            break;  
                        default:        //EMPTY  
                            System.out.print(" ");  
                    }  
                    System.out.print("|");  
                }  
                System.out.println("\n-------\n");  
            }  
        }  
          
        static void createBoard(int row, int col) {  
            gameboard = new int[row][col];  
        }  
          
         
          
        static boolean winOrTie() {  
          
          
              
          
            //Determine whether X or O won or there is a tie      
            return false;  
      
        }  
        /** 
         * @param args the command line arguments 
         */  
        public static void main(String[] args) {  
            Scanner scan = new Scanner(System.in);  
              
            createBoard(3,3);  
            int turn = 0;  
            int playerVal;  
            int outcome = -2;  
            boolean b = true;  
       
            boolean boolVal = true;  
      
    
     // int val = b? 1 : 0;  
              
              
              
            do {  
                displayBoard();  
                playerVal = (turn % 2 == 0)? NOUGHT : CROSS;  
                if (playerVal == NOUGHT) System.out.println("\n-O's turn-");  
                else System.out.println("\n-X's turn-");  
                System.out.print("Enter row and column:");  
                try {  
                        set(playerVal, scan.nextInt(), scan.nextInt());  
            } catch (IllegalArgumentException ex) {System.err.println(ex);}  
            turn ++;  
            outcome = winOrTie();  
            integer variable.
            return true;
                    
            } while ( outcome == -2 );  
            displayBoard();  
            switch (outcome) {  
                case NOUGHT:  
                    System.out.println("O wins!");  
                    break;  
                case CROSS:  
                    System.out.println("X wins!");  
                    break;  
                case EMPTY:  
                    System.out.println("Tie");  
                    break;  
                      
      
                      
                      
            }  
        }  
          
    }
    Last edited by Bykenhaal; December 18th, 2013 at 03:16 PM.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The slash in the end code tag goes the wrong way. It should be /

    winOrTie() needs to return an int, not a boolean, if you want to use the value returned in a switch statement with more than 2 cases.

    Define a variable with value: -2 vs hardcoding a -2 in several places in the code.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    59
    Rep Power
    1
    I am working on code that will catch any wrong input. So I do not get errors when I put in the wrong numbers.

    The other thing I am working on is a Scanner to check to see if O or X has won. If so I will say
    System.out.print O or X has won
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    winOrTie() needs to return an int, not a boolean, if you want to use the value returned in a switch statement with more than 2 cases.

    Define a variable with value: -2 vs hardcoding a -2 in several places in the code.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo