0

I am a noob programmer on JAVA and i'm trying to do one of my first programs and needless to say it's not going great. I have gotten rid of several error of the same type as this one, but i can't seem to be able to fix this. Without further ado here is what i need help with:

error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Main.createGame(Main.java:67)
at Main.processCommand(Main.java:116)
at Main.main(Main.java:40)`
`Exception in thread "main"` 

code:

import java.util.Scanner;

public class Main{

private static final String SAI = "SAI"; 
private static final String AJUDA = "AJUDA";
private static final String NOVO = "NOVO";
private static final String MAPA = "MAPA";
private static final String KNIGHT = "CAVALEIRO";
private static final String SWORDSMAN = "ESPADACHIM";
private static final String SPEARMAN = "LANCEIRO";
private static final String NORTH = "NORTE";
private static final String SOUTH = "SUL";
private static final String WEST = "OESTE";
private static final String EAST = "ESTE";
private static final String SAIR = "Obrigado por jogar. Ate a proxima.";

public static void main(String[] args) 
    {
          Scanner in = new Scanner(System.in);

          Game g1 = new Game();

          Units u1 = new Units();

          String command = "";

          String movement = "";


        System.out.println("novo - Novo jogo");
        System.out.println("ajuda - Mostra a ajuda");
        System.out.println("sai - Termina a execucao do programa");
        System.out.print("> ");

          while(!command.equals(SAI))
            {
                command = readCommand(in);
                movement = readMovement(in);
                processCommand(command, movement, g1, u1, in);
            }

          in.close();
    }

private static String readCommand(Scanner in)
{
    String command;

    command = in.next().toUpperCase();

    return command;
}

private static String readMovement(Scanner in)
{
    String movement;

    movement = in.next().toUpperCase();

    return movement;
}

private static void createGame(Game g1, Scanner in)
{
    int x = in.nextInt();
    int y = in.nextInt();
    String team1 = in.next();
    int f1x = in.nextInt();
    int f1y = in.nextInt();
    String team2 = in.next();
    int f2x = in.nextInt();
    int f2y = in.nextInt();
    in.nextLine();
    g1.createGame(x, y, f1x, f1y, f2x, f2y, team1, team2);
}

private static void processCommand(String command, String movement, Game g1, Units u1, Scanner in)
    {
      int tempflag = 0;

     switch (command)
        {
            case AJUDA:
                if(g1.on() == false)
                {
                in.nextLine();
                System.out.println("novo - Novo jogo");
                System.out.println("ajuda - Mostra a ajuda");
                System.out.println("sai - Termina a execucao do programa");
                System.out.print("> ");
                }
                else
                {
                    in.nextLine();
                    System.out.println("novo - Novo jogo");
                    System.out.println("mapa - Mostra o mapa do jogo");
                    System.out.println("cavaleiro - Move o cavaleiro");
                    System.out.println("espadachim - Move o espadachim");
                    System.out.println("lanceiro - Move o lanceiro");
                    System.out.println("ajuda - Mostra a ajuda");
                    System.out.println("sai - Termina a execucao do programa");
                    if((g1.turns()%2) == 0)
                    {
                        System.out.println(g1.Team1() + "> ");
                    }
                    else
                    {
                        System.out.println(g1.Team2() + "> ");
                    }
                }
                break;

            case NOVO:
                  g1.stop();
                  createGame(g1, in);
                  tempflag = (Math.abs(g1.flag1X() - g1.flag2X()) + Math.abs(g1.flag1Y() - g1.flag2Y()));
                  if(g1.altura() < 10 || g1.largura() < 10)
                  {
                      System.out.println("Mapa pequeno demais para o jogo");
                  }
                  if(g1.flag1X() < 2 || g1.flag1Y() < 2)
                  {
                      System.out.println(g1.Team1() + " bandeira em posicao invalida " + g1.flag1X() + g1.flag1Y());
                  }
                  if(g1.flag2X() < 2 || g1.flag2Y() < 2 || tempflag < 5)
                  {
                      System.out.printf(g1.Team2() + " bandeira em posicao invalida " + g1.flag2X() + g1.flag2Y());
                  }
                  if(g1.Team1() == g1.Team2())
                  {
                      System.out.println("As equipas nao podem ter o mesmo nome");
                  }
                  else
                  {
                      g1.start();
                      u1.knightAlive1();
                      u1.swordsmanAlive1();
                      u1.spearmanAlive1();
                      u1.knightAlive2();
                      u1.swordsmanAlive2();
                      u1.spearmanAlive2();
                      System.out.println("Jogo iniciado com sucesso, comeca a equipa " + g1.Team1());
                      System.out.print(g1.Team1() + "> ");
                  }
                  break;
IceWizard
  • 17
  • 2
  • Here you got 'java.util.InputMismatchException' it means your input is incorrect. Like you expecting a number but you entered one was not a number. Have a check your input sequence. – janith1024 Dec 10 '17 at 02:12
  • InputMismatchException - if the next token does not match the Integer regular expression, or is out of range. Are you entering number or something else? – Santosh Dec 10 '17 at 02:15
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking!"* –  Dec 10 '17 at 02:18
  • The error happens when i insert the input for the String team1, when i run the program and choose the command novo(create new game) and then insert the values for int x and int y everything goes smoothly, but when i try to insert the String it says error and i don't understand the issue there as i am inserting a String as asked – IceWizard Dec 10 '17 at 02:20

1 Answers1

-1

You should always ensure that your Scanner Object has the nextXXX(type) before calling attempting to get the desired type.

Your stack trace is very telling, "at java.util.Scanner.nextInt(Unknown Source) " is stating that it got something other than the expected type of int when it attempted to read. This occurs inside of your createGame() method, which looks like is called because the command "NOVO" was passed.

To avoid this error use the Scanners hasNextInt() method before attempting to get the next int within the stream. I.E if(hasNextInt()){....}

javaBean007
  • 555
  • 6
  • 11
  • 1
    It's dumb to downvote a correct answer simply due that somewhere else the question was also answered. If it's already answered somewhere else then don't allow up votes to my answer, a down vote indicates to users visibly(who may not even fully read the answer) that the answer is incorrect NOT a duplication of something already answered. – javaBean007 Dec 10 '17 at 02:26