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


public class Jogo {
 Scanner scanner = new Scanner(System.in);
int tipoJogo;
int turno;
Barco[] jfA,jfB;
Random random = new Random();

public Jogo(Barco[]fA, Barco[]fB, int tamanhomax, int tj)
{
    for(int i =0;i<tamanhomax;i++)
    {
        fA[i]=fB[i]=null;
    }

    jfA = fA;
    jfB = fB;
    tipoJogo=tj;
}



//public Barco[] getFrota() {
//  return frota;
//}
int linhaAC = random.nextInt(7);
int colunaAC = random.nextInt(7);
int orientação = random.nextInt(1);



public Jogo(int TipoJogo){
    TipoJogo = tipoJogo;
}
public  void manual1(){
    System.out.println("Jogador A");
    System.out.println("Insira as coordenadas do Aircraft Carrier e a sua orientação(0 horizontal, 1 vertical):");
    //linhaAC = scanner.nextInt();
    //colunaAC = scanner.nextInt();

    String op= scanner.nextLine();

    int linhaAC = (int) Character.toUpperCase(op.charAt(0))-'A'; //vai buscar o primeiro caracter da String op e transforma do A ao I em interiros 0 a 8
    int colunaAC = (int) op.charAt(1)-'1';
    int orientação = scanner.nextInt();
    System.out.println(linhaAC +"  -  " + colunaAC);


    do{

        System.out.println("Fora do tabuleiro");
        System.out.println("Insira de novo as coordenadas do Aircraft Carrier:  ");
        linhaAC= (int) Character.toUpperCase(op.charAt(0))-'A';
        colunaAC = (int) op.charAt(1)-'1';
        orientação = scanner.nextInt();

    }while((colunaAC > 4 && orientação == 0)|| (linhaAC > 'D' && orientação == 1));

Ok iam getting the following error code:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Jogo.manual1(Jogo.java:147)
at Main.start(Main.java:134)
at Main.main(Main.java:26)

This happen when i try to set a new value for variables LinhaAC and ColunaAC inside the do while loop.

Basically if i set LinhaAC to be greater than 'D' or ColunaAC to be greater than 4 it starts the Do while loop where i ask the user to insert new values to those variables.

So when i try to set new values, after i insert LinhaAC and ColunaAC i press enter so i can choose the new value for "orientação" it triggers the error.

It only happens inside the do while group. In the first time i insert the values it doesn't give any error.

EDIT: It happens even if i use the same value i choose in the first time. Its not like iam inserting an int on an scanner.next();

João Fróis
  • 89
  • 10

2 Answers2

0

There is no error in your code. The reason is that you get InputMismatchException because of your input is did not match with the variable type.

You are taking three inputs their types should be match.

That's the reason for these error messages:

at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)

As a Example:

orientação = scanner.nextInt();

This line expect integer value but if you add String to it gives you InputMismatchException

Solution:

To avoid this you can handle input type if it is not integer you can display proper message by try-catch block.

try{
    orientação = scanner.nextInt();
}catch(InputMismatchException e){
    System.out.println("Enter only integer");
}

Or you can print the error message like,

//...
catch(InputMismatchException e){
    System.out.println(e);
}

Likewise do this for every input you get from the keyboard. This will avoid error messages and make your program more easy to handle.

Learn more about exception handling.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

Finally i fix it.

public  void manual1(){
    System.out.println("Jogador A");
    System.out.println("Insira as coordenadas do Aircraft Carrier e a sua 
     orientação(0 horizontal, 1 vertical):");
    //linhaAC = scanner.nextInt();
    //colunaAC = scanner.nextInt();

    String op= scanner.next();

    int linhaAC = (int) Character.toUpperCase(op.charAt(0))-'A';
    int colunaAC = (int) op.charAt(1)-'1';
    int orientação = scanner.nextInt();
    System.out.println(linhaAC +"  -  " + colunaAC);


    while((colunaAC >= 4 && orientação == 0) || (linhaAC >= 4 && orientação == 1)){

        System.out.println("Fora do tabuleiro");
        System.out.println("Insira de novo as coordenadas do Aircraft Carrier:  ");
        op= scanner.next();
        linhaAC=(int)Character.toUpperCase(op.charAt(0))-'A';
        colunaAC = (int) op.charAt(1)-'1';
        orientação = scanner.nextInt();
        System.out.println(linhaAC +"  -  " + colunaAC);


    }
João Fróis
  • 89
  • 10