所以我有一个循环运行,它通过扫描仪和System.in从用户那里获取输入。扫描仪刚刚被称为s。我使用s.nextInt为我的交换机获取一个int,我有很多情况,所以用户有一些选择。现在在其中一种情况下,我使用另一个扫描仪,当我关闭它并且函数终止时(它将我带回我的while循环)我想从用户那里获取一个新的int,以选择他们想要的下一个东西做。这次它会抛出NoSuchElementException。我使用了“我之前”和“我之后”而userrequest = s.nextInt是我收到错误的地方。有任何想法吗?我的功能太长了不能发布,但除了打开一个新的扫描仪之外它并没有真正做任何应该干扰的事情,但我有其他功能可以做到这一点,但不会破坏它。
while (on) {
System.out.println("What may I do for you today? ");
System.out.println("I'm before");
int userrequest = s.nextInt();
System.out.println("I'm after");
switch (userrequest) {
case 1:
System.out.println("");........
编辑:好的,我从函数中删除了一些非重要的东西。这是它在返回我的while循环之前运行的函数:
public static void buysystem(Connection con, Scanner sysbuy) {
System.out.println("What system do you want to buy?");
String sysreq = sysbuy.next().toUpperCase();
try {
Statement st = con.createStatement();
String query = "SQL";
ResultSet rs = st.executeQuery(query);
rs.next();
String nome = rs.getString("noob");
int price = rs.getInt("cpuprice") + rs.getInt("ramprice") + rs.getInt("mainboardprice") + rs.getInt("casesprice") + rs.getInt("gfxprice");
System.out.println("How many systems do you want to buy?");
int sysamount = sysbuy.nextInt();
//NEED TO MAKE CHECK AMOUNT FUNCTION
int realprice;
if (sysamount>10){
realprice = (price*130/100)-((price*130/100*120/100)-(price*130/100));
System.out.println("You want to buy " + sysamount + " of the system: " + sysreq + " for the price of " + realprice + "?");
System.out.println("Yes/No?");
String yesno = sysbuy.next();
if (yesno.toUpperCase().equals("YES")){
System.out.println("You just bought " + sysamount + " of the system " + sysreq + " for the price of " + realprice);
//UPDATE DB
}
}
else{
realprice = (price*130/100)-((price*130/100*(sysamount*2+100-2)/100)-(price*130/100));
System.out.println("You want to buy " + sysamount + " of the system: " + sysreq + " for the price of " + realprice + "?");
System.out.println("Yes/No?");
String yesno = sysbuy.next();
if (yesno.toUpperCase().equals("YES")){
System.out.println("You just bought " + sysamount + " of the system " + sysreq + " for the price of " + realprice);
//UPDATE DB
}
}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Print me");
}
我现在收到此错误:
You just bought 1 of the system SERVER1 for the price of 7540
Print me
What may I do for you today?
I'm before
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at DBtest.requestHandler(DBtest.java:43)
at DBtest.main(DBtest.java:24)
答案 0 :(得分:0)
不要创建多个扫描仪,而是使用一个扫描仪并在功能中使用它,如果超出范围则将其传递给它。
您创建了多个扫描仪,您不应该这样做,但也关闭了第二个扫描仪。当您关闭扫描仪时,您关闭了两个扫描仪正在使用的输入流(系统的输入流)。这就是导致你崩溃的原因。