NoSuchElementException扫描程序不等待

时间:2018-04-30 20:11:52

标签: java nosuchelementexception

我正在为我的第一个学校服务器项目工作,当我在客户端访问下面的代码时,我收到NoSuchElementException。从我的理解,我写它的方式,扫描仪应该等待服务器发回一个字符串。相反,它似乎正在向异常跳跃。在服务器代码(下面的第二个)中,我有输出应该返回数组中的所有字符串。我的目标是让客户打印文本区域(状态)中的所有字符串。

    static void runClient() {
    Socket client = null;
    PrintWriter output = null;
    Scanner input = null;

    try {
        client = new Socket("localhost", 5007);

        input = new Scanner(client.getInputStream());
        output = new PrintWriter(client.getOutputStream());
        output.println(game);
        output.println(numberOfPicks);
        output.flush();
        pStr("Data Sent");

        while (true) {
            pStr("Waiting for Server");
            status.appendText(input.nextLine());
            if (!input.hasNext())
                break;
        }

    } catch (Exception e) {
        e.printStackTrace();

    } finally {
        try {
            input.close();
        } catch (Exception e) {
        }
        try {
            output.close();
        } catch (Exception e) {
        }
        try {
            client.close();
        } catch (Exception e) {
        }
    }
}

private static void pStr(String string) {
    System.out.println(string);

}
}

以下的部分服务器代码

        public void run() {
        PrintWriter output = null;
        Scanner input = null;
        try {
            // Get input and output streams.]
            input = new Scanner(connection.getInputStream());
            output = new PrintWriter(connection.getOutputStream());

            String game;
            int quickPicks;
            try {
                game = input.nextLine();
                quickPicks = Integer.parseInt(input.nextLine());

                switch (game) {
                case "PowerBall":
                    ansStr = new pickNumbers(game, quickPicks, 69, 26).getQuickPicks();
                    break;
                case "MegaMillions":
                    ansStr = new pickNumbers(game, quickPicks, 70, 25).getQuickPicks();
                    break;
                case "Lucky4Life":
                    ansStr = new pickNumbers(game, quickPicks, 48, 18).getQuickPicks();
                    break;

                default:
                    throw new RuntimeException("Incorrect Game");
                }
            } catch (Exception e) {
                output.println(e.getMessage());
            }
            for (int i = 0; i < ansStr.length; i++) {
                output.println(ansStr[i]);
                //output.flush();
            }
        } catch (Exception e) {
            pStr(e.getMessage());
        } finally {
            try {
                input.close();
            } catch (Exception e) {
            }
            try {
                output.close();
            } catch (Exception e) {
            }
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如何在status.appendText(input.nextLine());的测试中嵌套hasNextLine,例如:

if(input.hasNextLine()){
    status.appendText(input.nextLine());
}