NoSuchElementException Сканер не ждет - PullRequest
0 голосов
/ 30 апреля 2018

Я работаю над своим первым серверным проектом для школы и получаю 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 голосов
/ 01 мая 2018

Как насчет вложения status.appendText(input.nextLine()); в тест для hasNextLine например:

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