Java NoTouchElementException扫描仪未等待
我正在为学校做我的第一个服务器项目,当我在我的客户机中找到下面的代码时,我收到了一个Java NoTouchElementException扫描仪未等待,java,nosuchelementexception,Java,Nosuchelementexception,我正在为学校做我的第一个服务器项目,当我在我的客户机中找到下面的代码时,我收到了一个NoTouchElementException。根据我的理解,按照我的编写方式,扫描仪应该等待服务器发回字符串。相反,它似乎直接跳到了例外。在服务器代码(下面第二个)中,我有一个输出,它应该返回数组中的所有字符串。我的目标是让客户端打印文本区域(状态)中的所有字符串 下面是部分服务器代码 public void run() { PrintWriter output = null;
NoTouchElementException
。根据我的理解,按照我的编写方式,扫描仪应该等待服务器发回字符串。相反,它似乎直接跳到了例外。在服务器代码(下面第二个)中,我有一个输出,它应该返回数组中的所有字符串。我的目标是让客户端打印文本区域(状态)中的所有字符串
下面是部分服务器代码
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) {
}
}
}
}
public void run(){
PrintWriter输出=null;
扫描仪输入=空;
试一试{
//获取输入和输出流。]
输入=新扫描仪(connection.getInputStream());
输出=新的PrintWriter(connection.getOutputStream());
弦乐游戏;
快速挑选;
试一试{
game=input.nextLine();
quickPicks=Integer.parseInt(input.nextLine());
切换(游戏){
案例“强力球”:
ansStr=新的拾取编号(游戏,快速拾取,69,26);
打破
“百万”案:
ansStr=新的拾取编号(游戏,快速拾取,70,25).getQuickPicks();
打破
案例“Lucky4Life”:
ansStr=新的拾取编号(游戏,快速拾取,48,18);
打破
违约:
抛出新的RuntimeException(“不正确的游戏”);
}
}捕获(例外e){
println(e.getMessage());
}
for(int i=0;i
嵌套如何status.appendText(input.nextLine())测试hasNextLine
时的code>,例如:
if(input.hasNextLine()){
status.appendText(input.nextLine());
}
if(input.hasNextLine()){
status.appendText(input.nextLine());
}