我收到以下错误
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at task1.ControlApplication2.uQueue(ControlApplication2.java:59)
at task1.ControlApplication2.main(ControlApplication2.java:20)
错误出现在较大的程序中,在第一次使用扫描程序后,调用一个方法并返回循环重新运行。只有在事先几乎立即返回一个方法之后才会发生这种情况。
public static void operations(PriorityQueueADT<String> pQueue) {
Scanner userInput2 = new Scanner(System.in);
boolean exit = false;
do {
try {
System.out.println("Which operation would you like to perform?");
System.out.println("1) - Enqueue");
System.out.println("2) - Dequeue");
System.out.println("3) - Get the item of the higest priority");
System.out.println("4) - Check Queue Size");
System.out.println("5) - Check if empty");
System.out.println("6) - Exit to Menu");
String A = userInput2.nextLine();
int option = Integer.parseInt(A);//Line 59
switch (option) {
case 1: inputChoice(pQueue);
break;
case 2: System.out.println("The item removed is: "+pQueue.dequeue());
break;
case 3: System.out.println("The highest priority item is: "+pQueue.getHighest());
break;
case 4: System.out.println("The queue size is: "+pQueue.size());
break;
case 5: System.out.println("The queue is: "+((pQueue.isEmpty())? "Empty" : "Not Empty"));
break;
case 6: exit = true;
break;
default: System.out.println("You made an invalid choice, please try again.");
}
}
catch (EmptyQueueException e) {
System.out.println("The queue is empty");
}
} while (exit != true);
userInput2.close();
return;
}
public static void inputChoice(PriorityQueueADT<String> pQueue) {
Scanner userInput3 = new Scanner(System.in);
System.out.print("Would you like to enter the details from a file (Y/N)");
String f = userInput3.nextLine();
boolean file = (((f.equals("Y")) || (f.equals("y"))) ? true : false);
if (file) {
if (fileInput(pQueue)) {}
else {
System.out.println("Reading from the file failed. Please enter the data manually");
manInput(pQueue);
}
} else manInput(pQueue);
userInput3.close();
}
public static boolean fileInput(PriorityQueueADT<String> pQueue) {
Scanner n = new Scanner(System.in);
boolean exit = false;
do {
System.out.print("Please enter the name of the file to be read from: ");
String name = n.nextLine();
try {
File file = new File(name);
Scanner fr = new Scanner(file);
while (fr.hasNextLine()){
String line = fr.nextLine();
String[] element = line.split(" ");
if (element.length != 2) throw new InputMismatchException();
pQueue.enqueue(element[0], Integer.parseInt(element[1]));
}
fr.close();
exit = true;
}
catch (FileNotFoundException e) {
System.out.println("The file could not be found"+name);
System.out.println("Please enter a new file name");
}
catch (InputMismatchException f) {
System.out.println("The file is incomplete or corrupted");
n.close();
return false;
}
} while (exit !=true);
n.close();
return true;
}
public static void manInput(PriorityQueueADT<String> pQueue) {
Scanner userInput4 = new Scanner(System.in);
boolean exit = false;
do {
System.out.print("Would you like to add an item? (y/n)");
if (userInput4.nextLine().equals("y") || userInput4.nextLine().equals("Y")) {
System.out.print("Please enter the item");
String item = userInput4.nextLine();
System.out.print("Please enter the priority");
int priority = Integer.parseInt(userInput4.nextLine());
pQueue.enqueue(item, priority);
} else exit = true;
} while (exit == false);
userInput4.close();
}
执行输入选择后,它是后续的子方法,循环返回到开头,userInput2.nextLine()
出现错误,我仍然无法解释。当我选择从操作方法返回main方法时,会出现相同的错误。
`Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at task1.ControlApplication2.main(ControlApplication2.java:18)`
`public static void main(String[] args) {
Scanner userInput1 = new Scanner(System.in);
boolean exit = false;
do {
System.out.println("Please select the implementation of" +
"the Priority Queue which you would like to use");
System.out.println("1) - Unsorted Priority Queue");
System.out.println("2) - Sorted Priority Queue");
System.out.println("3) - Sorted Queue of Queues");
System.out.println("4) - Quit");
int option = Integer.parseInt(userInput1.nextLine());//Line 18
switch (option) {
case 1: uQueue();
break;
case 2: sQueue();
break;
case 3: qQueue();
break;
case 4: exit = true;
break;
default: System.out.println("You made an invalid choice, please try again.");
}
} while (exit != true);`
在我尝试输入之前它会这样做。