java.util.InputMismatchException是什么意思

时间:2017-12-10 01:55:13

标签: java java.util.scanner

我是JAVA的noob程序员,我正在尝试做我的第一个程序之一,不用说它不会很好。我已经摆脱了与此相同类型的几个错误,但我似乎无法解决这个问题。 在这里,我需要帮助:

错误:

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"` 

代码:

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;

1 个答案:

答案 0 :(得分:-1)

在调用尝试获取所需类型之前,应始终确保您的扫描器对象具有nextXXX(类型)。

您的堆栈跟踪非常有说服力,&#34;在java.util.Scanner.nextInt(未知来源) &#34;表示当它试图读取时,它得到的东西不是预期的int类型。这发生在你的createGame()方法中,看起来像是因为命令&#34; NOVO&#34;过了。

要避免此错误,请在尝试获取流中的下一个int之前使用Scanners hasNextInt()方法。 I.E if(hasNextInt()){....}