Scannerに問題があります。入力値の型を取っていると思われ、次回ユーザーが同じ型の値を入力したように見えます。私はこのコードが動作していない理由を見つけることができず、私はこの100万回のようなコードを書いて問題がなかったので、私にInputMismatchExceptionを与えます。この問題は、ちょうどこのコードでレジスタ()メソッドが、プログラム全体、例えばのためではありませんInputMismatchExceptionの理由が見つかりません
public void register(){
Scanner input=new Scanner(System.in);
System.out.println("What course would you like to register for?");
String course_name = input.next();
System.out.println("What section?");
int section = input.nextInt();
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getCourse_name().equals(course_name)) {
if (courses.get(i).getCourse_section() == section) {
courses.get(i).AddStudent(this.first_name+" "+this.last_name);
}
}
}
input.close();
}
は:
public void Options() {
Scanner input=new Scanner(System.in);
while (true) {
System.out.println("What would you like to do (Enter corresponding number):" + "\n" + "1) View all courses" + "\n" + "2) View all courses that are not full" + "\n" + "3) Register on a course" + "\n" + "4) Withdraw from a course" + "\n" + "5) View all courses that the current student is being registered in" + "\n" + "6) Exit");
int user = input.nextInt();
if (user == 1)
viewAll();
if (user == 2)
viewAllOpen();
if (user == 3)
register();
if (user == 4)
withdraw();
if (user == 5)
viewRegistered();
if (user == 6) {
Serialize();
break;
}
}
レジスタ等の方法の一つは、入力するように、ユーザが必要な場合文字列、intユーザー= input.nextInt(); InputMismatchExceptionが発生します。
チェックhttp://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – TheLostMind