2016-09-28 78 views
0

我目前正在研究一個從Google表單獲取信息並運行計算的程序。我使用「gradle -q run」來運行我的程序,我希望它在終端內提示用戶。現在我正在使用掃描儀,但它說:「輸入你的名字:線程中的異常」main「java.util.NoSuchElementException」當我運行而不是等待我添加輸入時,我該如何解決這個問題?命令提示符下的提示用戶

public static void main(String[] args) throws IOException { 
     // Build a new authorized API client service. 
     Sheets service = getSheetsService(); 

     // Prints the names and majors of students in a sample spreadsheet: 
     // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit 
     String spreadsheetId = "1Pfs-EpysJZXibe1pHiPLEx8cOCC86H5he5ouApejqiE"; 
     String range = "Sheet1!A:E"; 

    // create a scanner so we can read the command-line input 
     Scanner scanner = new Scanner(System.in); 

     // prompt for the user's name 
     System.out.print("Enter your name: "); 

     // get their input as a String 
     String username = scanner.nextLine(); 

     // prompt for their age 
     System.out.print("Enter your age: "); 

     // get the age as an int 
     int age = scanner.nextInt(); 

     System.out.println(String.format("%s, your age is %d", username, age)); 


     ValueRange response = service.spreadsheets().values() 
      .get(spreadsheetId, range) 
      .execute(); 
     List<List<Object>> values = response.getValues(); 
     if (values == null || values.size() == 0) { 
      System.out.println("No data found."); 
     } else { 
      System.out.println("Dominik's Awesome Script Says:"); 
      System.out.println(""); 
      for (List row : values) { 
      // Print columns A and E, which correspond to indices 0 and 4. 
      System.out.printf("%s, %s\n", row.get(0), row.get(4)); 
      } 
     } 
    } 

回答

0

嘗試使用

while(scanner.hasNextLine()){ 
    username = scanner.nextline(); 
} 

它爲我的https://www.compilejava.net/

+0

它不會等我鍵入雖然它只是認爲該條件爲假,並不斷去。 – MickDom