I'm receiving an error on run
Okay so I've encountered an error and I'm not so sure why, any help would be appreciated. When I try run it, I'm met with the following.
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at JonesInc.main(JonesInc.java:16)
public class JonesInc
{
public static void main(String[] args)
{
Scanner kbReader=new Scanner(System.in);
System.out.println("1. Administration");
System.out.println("2. Check Stock Availability");
System.out.println("3. Purchase Textbooks");
System.out.println("4. Return Textbooks");
System.out.println("5. Quit");
System.out.println("please select your choice");
int choice=kbReader.nextInt();
int stock=100;
if(choice==1)
{
Scanner password=new Scanner(System.in);
System.out.println("Please Enter Password");
String name=password.nextLine();
if(name.equals("Java"))
System.out.println("A. Increase Stock Availabilty");
System.out.println("B. Reduce Stock Availability");
System.out.println("C. Quit");
System.out.println("Select your choice");
Scanner kbReader1=new Scanner(System.in);
String choice1=kbReader1.nextLine();
char ch=choice1.charAt(0);
System.out.println("");
switch (ch)
{
case'A':
case'a':
System.out.println("How much do you wish to increase stock by");
break;
case'B':
case'b':
System.out.println("How much do you wish to decrease stock by");
break;
case'C':
case'c':
System.out.println("Dave is cool");
break;
}
}
}
}
You're running the program (which seems to work correctly, at least superficially) in some way which doesn't allow interactive input.
The Scanner
tries to read here:
int choice=kbReader.nextInt();
but System.in
is already closed.
You should run the program in a way that supports inputting data via System.in
, eg in any major IDE.
上一篇: 从文本文件读取到JTextfield
下一篇: 我在运行时收到错误