2015-12-07 61 views
-1

我可以從用戶的scan2 scanner.But掃描不會產生在此代碼中我怎樣才能進入這while while循環我無法從用戶掃描掃描儀獲得輸入。我如何從用戶獲得掃描儀掃描儀的輸入。Java從用戶獲得輸入

Scanner scan2 = new Scanner(System.in); 
    System.out.println("Register Section\nID: "); 
    String id = scan2.next(); 
    System.out.println("Password: "); 
    String pass = scan2.next(); 
    createAccount(id, pass, accounts); 
    System.out.println("Login Section\nID: "); 
    id = scan2.next(); 
    System.out.println("Password: "); 
    pass = scan2.next(); 
    boolean X = logIN(id, pass, students); 
    scan2.close(); 

//Login succeed 

    if(X){ 

    Scanner scan = new Scanner(System.in); 
    String line = ""; 

    while (scan.hasNextLine()) {  
     line = scan.nextLine(); 
     if (line.equalsIgnoreCase("quit")) 
      System.exit(0); 

     int timetablestart = line.indexOf('['); 
     int timetablefinish = line.indexOf(']'); 

     String strlistoftimetable = ""; 
     String command = line; 
     String[] timetableslots = null; 
     if ((timetablestart > 0) && (timetablefinish > 0)) { 
      strlistoftimetable = line.substring(timetablestart + 1, 
        timetablefinish); 
      timetableslots = strlistoftimetable.split(", "); 
      command = line.substring(0, timetablestart - 1); 
     } 

     String[] tokens = command.split("\\s"); 
     if (tokens.length < 2) { 
      System.out.println("something is wrong!"); 
      return; 
     } 



     if (tokens[0].equals("add")) { 
      if (tokens[1].equals("O")) { 

       CSDriver.addO(tokens,timetableslots,departments); 


      } else if (tokens[1].equals("C")) { 

       CSDriver.addC(tokens, students); 

      } else if (tokens[1].equals("I")) { 

       CSDriver.addI(tokens,instructors); 


      } 

     } 

     else if (tokens[0].equals("print")) { 

      if (tokens[1].equals("S")) { 

       CSDriver.printS(tokens, students); 


      } else if (tokens[1].equals("I")) { 

       CSDriver.printI(tokens,instructors); 

      } 
       else if (tokens[1].equals("Announcement")) { 

       CSDriver.printAnnouncement(departments); 

      } 
      else if (tokens[1].equals("AcedemicActivities")) { 

CSDriver.printAcedemicActivities(departments);} 
      else if (tokens[1].equals("Attendance")) { 

        CSDriver.checkAttendance(tokens, students); 

       } 
      else if (tokens[1].equals("Internship")) { 

       CSDriver.checkInternship(tokens, students); 

      } 

      else if (tokens[1].equals("SemesterGrades")) { 

       CSDriver.checkSemesterGrades(tokens, students); 
      } 
     }   else 
      System.out.println("something is wrong!"); 


    } 

    scan.close(); 
} } 
+1

我們需要看到您的「登錄」的方法。很可能不會將布爾X設置爲true – bpgeck

+0

line = scan.nextLine();即使X = true也無法正常工作 –

+0

請包括重現問題所需的最短程序。我們不能用您提供的代碼重現問題。 –

回答

3

你是不是從scan得到任何東西,因爲你已經關閉System.in(收盤scan2時,在「登錄成功」部分的結束)。您不應該自己關閉它,因爲它會阻止將來嘗試從此流讀取數據。

+0

謝謝你幫助我。沒有問題了 –

0

當您使用

Scanner scan2 = new Scanner(System.in); 
scan2.close(); 

你關閉SCAN2System.in,所以爲了糾正你的問題,你需要在你的程序結束時關閉您的掃描儀。

移動這條線在年底

scan2.close(); 
+0

謝謝你幫助我。沒有問題了 –