-2

これに何か問題がある場合はお知らせください。このエラーが発生しています:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at interaction.menu(interaction.java:15)
    at driver.main(driver.java:9)

15 行目はselection = scan.nextInt();、while ループのすぐ内側にあります。メインには、このクラスでこのメソッドを呼び出すメソッドが含まれているだけです。

//provides the interface to be used
    public void menu(){
    Scanner scan = new Scanner(System.in);
    database db = new database();
    int selection;

    while(true){
        hugeTextBlock();
        selection = scan.nextInt();
        switch(selection){
            //creates a new course
            case 1: db.addCourse();
            //removes a course
            case 2: db.deleteCourse();
            //enroll a student
            case 3: db.enrollStudent();
            //delete a student
            case 4: db.deleteStudent();
            //register for a course
            case 5: db.registerStudent();
            //drop a course
            case 6: db.dropCourse();
            //check student registration
            case 7: db.checkReg();
            //quit
            case 8: break;  
            default: System.out.println("default action");
        }
    }
}

以下は、別のクラス内の addCourse メソッドです。私はそれを単独で実行しましたが、問題なく動作します。

//creates a new course
public void addCourse(){
    try{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:StudentRegistration_DSN");
    Statement st = conn.createStatement();
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter the course title: ");
    String title = scan.nextLine();
    System.out.println("Please enter the course's code: ");
    String code = scan.next();

    st.executeUpdate("insert into course values('"+code+"','"+title+"')");
    ResultSet rs = st.executeQuery("select * from course");
    code = "";
    title = "";
    System.out.println("This is the relation as of current changes.");

    while (rs.next())
    {
       code=rs.getString(1);
       title=rs.getString(2);
       System.out.println("Code: " + code + "   Title: " + title);
    }
    rs.close();
    st.close();
    conn.close();
    scan.close();
    }
    catch (Exception e){
        System.out.println(e);
    }

}
4

2 に答える 2

1

まず、ケース8のスイッチを壊すだけでおかしなことが起こります。すべてのケースの後にブレークを追加System.exit(0)し、ケース 8 に追加する必要があります。

次に、スキャナーのプロンプトで何か入力しましたか? 入力終了記号を入力すると、これが発生します。System.inまた、対応するストリームは何ですか?正規のコマンド ラインからこれを呼び出して、入力の終わりを入力しないと、どのようにこれが発生するのかわかりません。

于 2013-07-25T19:42:53.793 に答える
0

例外はScanner.nextInt、読み取る int がないことを示しています。スキャナが次に返すものが、実際には int であることを確認する必要があります。Scanner.hasNextIntを参照してください。

while (!scanner.hasNext()) {
    // sleep here
}
if (scanner.hasNextInt()) {
   selection = scan.nextInt();
} else {
    selection = 0;
    scan.next();  // reads the garbage.
}
于 2013-07-25T19:42:36.090 に答える