NoSuchElementException 错误
Error in NoSuchElementException
有人可以帮助我吗:我收到以下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at I6Exc2.menuSelection(I6Exc2.java:28)
at I6Exc2.PersonsWrite(I6Exc2.java:120)
at I6Exc2.menuSelection(I6Exc2.java:43)
at I6Exc2.main(I6Exc2.java:19)
当我的第一个输入是 3 而我的下一个输入是 5 时会发生这种情况。看起来我在关闭扫描仪时做错了什么?有人能帮我吗?
非常感谢。
public class Abc {
public static Person[] names;
public static void main(String[] args) {
menuSelection();
}
public static void menuSelection() {
Scanner s = new Scanner(System.in);
System.out.println( "Choose menu item:" + "\n" + "1. Read File"
+ "\n" + "2. Creates nr objects" + "\n" + "3. Write a File"
+ "\n" + "4. Display nr objects" + "\n" + "5. Exit");
int menuSelection = s.nextInt();
switch (menuSelection) {
case 1: System.out.println("Input a name");
String filePerson = s.next();
PersonRead(filePerson);
break;
case 2: System.out.println("Input nbr of obj");
int p = s.nextInt();
PersonsCreate(p);
break;
case 3: System.out.println("Input a name");
String filePersonWrite = s.next();
PersonWrite(names, filePersonWrite);
break;
case 4: PersonsDisplay(names);
break;
case 5: System.out.println("Good Bye!");
s.close();
break;
default: System.out.println("Invalid choice");
menuSelection();
break;
}
}
public static Person[] PersonRead (String filePerson) {
Person[] names2 = names;
try (FileInputStream fi = new FileInputStream(filePerson)) {
ObjectInputStream os = new ObjectInputStream(fi);
names2 = (Person[])os.readObject();
os.close();
} catch (IOException e) {
System.out.println("Person file not found.");
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("File " + filePerson + " does not contains valid Person object");
}
names = names2;
if (names != null) {
System.out.println("p Person read successfully from file " + filePerson);
}
menuSelection();
return names;
}
public static Person[] PersonsCreate(int p) {
names = new Person[p];
for(int i=0; i < p; i++) {
names[i] = new Person("Mr. Tim" + i, 20 + i, 'M');
}
menuSelection();
return names;
}
public static void PersonWrite (Person[] Person, String filePerson) {
if (names != null) {
try (FileOutputStream fs = new FileOutputStream(filePerson)) {
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(names);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("p Person written successfully to " + filePerson);
} else {
System.out.println("Nothing to write.");
}
menuSelection();
}
public static void PersonsDisplay(Person[] Person) {
for(Person names: Person) {
System.out.println(names);
}
menuSelection();
}
}
NoSuchElementException Thrown by the nextElement, if no more tokens are available
使用 hasNextInt()
、
检查
if(input.hasNextInt() ){
int p = s.nextInt();
}
有人可以帮助我吗:我收到以下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at I6Exc2.menuSelection(I6Exc2.java:28)
at I6Exc2.PersonsWrite(I6Exc2.java:120)
at I6Exc2.menuSelection(I6Exc2.java:43)
at I6Exc2.main(I6Exc2.java:19)
当我的第一个输入是 3 而我的下一个输入是 5 时会发生这种情况。看起来我在关闭扫描仪时做错了什么?有人能帮我吗? 非常感谢。
public class Abc {
public static Person[] names;
public static void main(String[] args) {
menuSelection();
}
public static void menuSelection() {
Scanner s = new Scanner(System.in);
System.out.println( "Choose menu item:" + "\n" + "1. Read File"
+ "\n" + "2. Creates nr objects" + "\n" + "3. Write a File"
+ "\n" + "4. Display nr objects" + "\n" + "5. Exit");
int menuSelection = s.nextInt();
switch (menuSelection) {
case 1: System.out.println("Input a name");
String filePerson = s.next();
PersonRead(filePerson);
break;
case 2: System.out.println("Input nbr of obj");
int p = s.nextInt();
PersonsCreate(p);
break;
case 3: System.out.println("Input a name");
String filePersonWrite = s.next();
PersonWrite(names, filePersonWrite);
break;
case 4: PersonsDisplay(names);
break;
case 5: System.out.println("Good Bye!");
s.close();
break;
default: System.out.println("Invalid choice");
menuSelection();
break;
}
}
public static Person[] PersonRead (String filePerson) {
Person[] names2 = names;
try (FileInputStream fi = new FileInputStream(filePerson)) {
ObjectInputStream os = new ObjectInputStream(fi);
names2 = (Person[])os.readObject();
os.close();
} catch (IOException e) {
System.out.println("Person file not found.");
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("File " + filePerson + " does not contains valid Person object");
}
names = names2;
if (names != null) {
System.out.println("p Person read successfully from file " + filePerson);
}
menuSelection();
return names;
}
public static Person[] PersonsCreate(int p) {
names = new Person[p];
for(int i=0; i < p; i++) {
names[i] = new Person("Mr. Tim" + i, 20 + i, 'M');
}
menuSelection();
return names;
}
public static void PersonWrite (Person[] Person, String filePerson) {
if (names != null) {
try (FileOutputStream fs = new FileOutputStream(filePerson)) {
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(names);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("p Person written successfully to " + filePerson);
} else {
System.out.println("Nothing to write.");
}
menuSelection();
}
public static void PersonsDisplay(Person[] Person) {
for(Person names: Person) {
System.out.println(names);
}
menuSelection();
}
}
NoSuchElementException Thrown by the nextElement, if no more tokens are available
使用 hasNextInt()
、
if(input.hasNextInt() ){
int p = s.nextInt();
}