Int field as string

I'm working on a code for a college job, but I'm having problems, despite the" house " being set to INT, netbeans continues to present:

"incompatible types: String cannot be converted to int"

Below follows the code, from now on, Thank you very much!

package tictactoe;

import java.util.Scanner;
import java.util.Random;

/**
 *
 * @author Leonardo
 */
public class Main {

    private static String tipoJogo;
    private static Random rand = new Random();
    private static int  n = rand.nextInt(9) + 1;
    private static int casa;
    public static void main(String[] args) {
        Scanner carrega = new Scanner(System.in);

        System.out.println("Bem vindo!");
        System.out.println("Insira o tipo de Jogo");
        tipoJogo = carrega.next();

        int valida = 0,
                jogadas = 0;

        jogo jogo = new jogo();


        switch (tipoJogo) {

            case "humano":

                while (true) {
                    System.out.println("teste teste");

                    do {
                        jogo.varTabu();
                        System.out.printf("Primeiro Jogador, ensira sua "
                                + "jogada nas casas de 1 a 9 \n\r");
                        casa = carrega.next();
                        while (!jogo.ok(casa)) {
                            System.out.print("Casa já escolhida, tente outra!");
                            System.out.printf("Primeiro Jogador, ensira sua "
                                    + "jogada nas casas de 1 a 9");
                            casa = carrega.next();
                            valida = 0;

                        }
                        jogo.jogada(casa, "O");
                        valida = 1;

                    } while (valida == 0);
                    jogadas++;
                    valida = 0;
                    if (!jogo.ganhador(jogadas).equals("null")) {
                        break;
                    }
                    do {
                        jogo.varTabu();
                        System.out.printf("Segundo Jogador, ensira sua "
                                + "jogada  nas casas de 1 a 9\n\r");
                        casa = carrega.next();
                        while (!jogo.ok(casa)) {
                            System.out.print("Casa já escolhida, tente outra!");
                            System.out.printf("Segundo Jogador, ensira sua "
                                    + "jogada /n/r nas casas de 1 a 9");
                            casa = carrega.next();
                            valida = 0;

                        }
                        jogo.jogada(casa, "X");
                        valida = 1;

                    } while (valida == 0);
                    jogadas++;
                    valida = 0;
                    if (!jogo.ganhador(jogadas).equals("null")) {
                        break;
                    }
                }
                jogo.varTabu();
                System.out.printf("O campeão foi o " + jogo.ganhador(jogadas));
                break;
            case "cFacil":

                while (true) {
                    System.out.println("teste teste");

                    do {
                        jogo.varTabu();
                        System.out.printf("Primeiro Jogador, ensira sua "
                                + "jogada nas casas de 1 a 9 \n\r");
                        casa = carrega.next();
                        while (!jogo.ok(casa)) {
                            System.out.print("Casa já escolhida, tente outra!");
                            System.out.printf("Primeiro Jogador, ensira sua "
                                    + "jogada nas casas de 1 a 9");
                            casa = carrega.next();
                            valida = 0;

                        }
                        jogo.jogada(casa, "O");
                        valida = 1;

                    } while (valida == 0);
                    jogadas++;
                    valida = 0;
                    if (!jogo.ganhador(jogadas).equals("null")) {
                        break;
                    }
                    System.out.println("pudim");

                    break;
                }
                /* inicio parte computador           
                //////////////////////////////////
                //////////////////////////////////
                //////////////////////////////////
                /////////////////////////////////                
                 */
                do {
                    jogo.varTabu();

                    System.out.printf("Segundo Jogador, ensira sua "
                            + "jogada  nas casas de 1 a 9\n\r");
                    n = carrega.next();
                    while (!jogo.ok(casa)) {
                        System.out.print("Casa já escolhida, tente outra!");
                        System.out.printf("Segundo Jogador, ensira sua "
                                + "jogada /n/r nas casas de 1 a 9");
                        casa = carrega.next();
                        valida = 0;

                    }
                    jogo.jogada(casa, "X");
                    valida = 1;

                } while (valida == 0);
                jogadas++;
                valida = 0;
                if (!jogo.ganhador(jogadas).equals("null")) {
                    break;
                }
                break;
        }
        jogo.varTabu();
        System.out.printf("O campeão foi o " + jogo.ganhador(jogadas));

    }
}
Author: Leonardo Lemgruber, 2016-10-07

1 answers

The Scanner method next() returns a string delimited to the first space. Therefore, you cannot assign this string data entry capture to an int variable.

Change all:

casa = carrega.next();

For:

casa = carrega.nextInt();

The Line n = carrega.next(); will probably give this same problem, since n has been declared as int, make the same change to solve the problem.

Since there is a data capture loop, it is quite likely that you have problems with leaking a line break of this call, take advantage and give a read in this question if this occurs.

References for reading:

Scanner Class (Documentation)

Data Entry: Scanner class

 3
Author: , 2016-10-07 14:55:40