2013-07-25 57 views
-2

让我知道你们是否看到有什么问题。我得到这个错误:简单的switch语句错误?

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();正确的内循环。主要包含一个在这个类中调用这个方法的方法。

//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

您可能应该在您的交换机中有一些中断... –

+0

请参阅http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html。 (尤其是,搜索该页面的短语*通过*。) – ruakh

回答

0

例外情况是,Scanner.nextInt没有可读的整数。您应该确保扫描仪将返回的下一个事实上是一个整数。见Scanner.hasNextInt

while (!scanner.hasNext()) { 
    // sleep here 
} 
if (scanner.hasNextInt()) { 
    selection = scan.nextInt(); 
} else { 
    selection = 0; 
    scan.next(); // reads the garbage. 
} 
+0

请原谅我的无知,但它不会停止输入另一个整数。它只是完成了方法和崩溃。我认为案件会结束,while循环会像第一次那样重新开始一切。我应该做一个语句来检查“if(scan.hasNextInt)”吗?如果没有,做另一个扫描? – waltershc

+0

编辑:如果我使用另一种情况,switch语句正常工作。 while循环只是让它继续下去。另外,'db.addCourse();'在switch语句之外工作。当方法结束时会发生什么,会扰乱扫描器对象? – waltershc

+0

异常会停止当前线程。它不会再运行了。您需要检查是否有输入,以及输入是否是整数。并据此阅读。我会更新答案。 –

1

首先,只有打破情况8的开关会导致奇怪的事情发生。您应该在每个案例后添加一个中断,并为案例8添加System.exit(0)

其次,您是否在扫描器提示处键入了任何内容?如果你输入一个输入结束符号,就会发生。另外,System.in对应什么流?如果你是从真正的命令行调用它并且不输入输入的结尾,我不会看到这会发生。

+0

我只是使用Eclipse内部的控制台。我经历了并增加了休息和退出命令。我在扫描仪提示处输入了什么?第一次,它做我想做的任何事情。然后,在完成该任务后,它会通过原始while循环返回并崩溃,而不要求我另外输入。 – waltershc