2014-07-20 139 views
-3

好的,所以這個代碼的問題是,如果它不止一次地遍歷循環,關閉掃描器會導致代碼忽略掃描以進一步用戶輸入。我知道這個問題必須是Scanner課程中的內部問題,但是有沒有人有辦法以更安全的方式關閉掃描儀? (這可能有助於複製代碼,並嘗試輸入「55」或「j」,例如,如果您沒有意識到我馬上就會遇到的問題)。Java.util.Scanner,問題關閉

public static void main(String[] args){ 
    int numCom = 0, count = 0; 
    boolean valid = false; 

    do{ 
     Scanner num = new Scanner(System.in); 
     try{ 
      if(count == 0){ 
       System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
       count++; 
      }else{ 
       System.out.println("Error!\nPlease enter a number between 1-8."); 
      } 
      numCom = num.nextInt(); 
     }catch(Exception e){} 
     if(numCom < 9 && numCom > 0){ 
      valid = true; 
     } 
     num.close(); 
    }while(!valid); 
}//main() 
+0

你的評論上,除了我的所有問題的答案。有什麼問題嗎? – Boann

+0

對不起,我有一段時間沒有真正在這個網站上(最近一直流暢的航行)。我已經試圖關閉掃描儀,因爲它也關閉了輸入流(這種情況下的System.in)。如果有一種方法可以在事後重新打開流,那很好。 –

+0

我已經回答了所有這些...... – Boann

回答

0

創建並關閉掃描儀以外的循環。就像這樣:

public static void main(String[] args) { 
     int numCom = 0, count = 0; 
     boolean valid = false; 
     Scanner num = new Scanner(System.in); 

     do { 
      try { 
       if (count == 0) { 
        System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
        count++; 
       } else { 
        System.out.println("Error!\nPlease enter a number between 1-8."); 
       } 
       numCom = num.nextInt(); 
      } catch (Exception e) {} 
      if (numCom < 9 && numCom > 0) { 
       valid = true; 
      } 
     } while (!valid); 
     num.close(); 
    } //main() 
+0

不好,嘗試一下,但輸入的值會導致循環不止一次。這是我的第一本能,但如果你不關閉掃描儀,它甚至不能正常工作,請嘗試一下 –

+0

你是否嘗試過使用代碼的函數而不是在你的主函數中? – simeg

+0

是的,我只是複製/粘貼到臨時班,實際上;在我的完整程序中它是一個獨立的功能。 –

0

試試這個:JAVA 7+ http://ideone.com/dxtXmz

public static void main(String[] args){ 
    int numCom = 0, count = 0; 
    boolean valid = false; 
     try (Scanner num = new Scanner(System.in)) { 
      do{ 
       try{ 
        if(count == 0){ 
         System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
         count++; 
        }else{ 
         System.out.println("Error!\nPlease enter a number between 1-8."); 
        } 
        numCom = num.nextInt(); 
       }catch(Exception e){} 
       if(numCom < 9 && numCom > 0){ 
        valid = true; 
       } 
      }while(!valid); 
     } 
}//main() 

OR

試試這個:http://ideone.com/LaN6kF

public static void main(String[] args){ 
int numCom = 0, count = 0; 
boolean valid = false; 

    Scanner num = new Scanner(System.in); 
do{ 
    try{ 
     if(count == 0){ 
      System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
      count++; 
     }else{ 
      System.out.println("Error!\nPlease enter a number between 1-8."); 
     } 
     numCom = num.nextInt(); 
    }catch(Exception e){} 
    if(numCom < 9 && numCom > 0){ 
     valid = true; 
    } 
}while(!valid); 

    num.close(); 
}//main() 

基本資源:掃描儀:分配只有一次和重用。 當不需要時,它關閉。

+0

是啊,已經嘗試過,事實上,並沒有運氣不幸的是.. –

+0

它工作得很好:http://ideone.com/dxtXmz –

+0

http://ideone.com/LaN6kF –

0

如果您需要處理字符串爲好,試試這個:http://ideone.com/f7IMwD

public static void main(String[] args) { 
     int numCom = 0, count = 0; 
     boolean valid = false; 
     try (Scanner num = new Scanner(System.in)) { 
      do { 
       try { 
        if (count == 0) { 
         System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
         count++; 
        } else { 
         System.out.println("Error!\nPlease enter a number between 1-8."); 
        } 
        String line = num.nextLine(); 
        numCom = Integer.parseInt(line); 
        if (numCom < 9 && numCom > 0) { 
         valid = true; 
        } 
       } catch (NumberFormatException e) { 
        valid = false; 
       } 

      } while (!valid); 
     } 
    }//main() 
+0

是的,這看起來它可能實際上有潛力。只是希望有一種不那麼乏味的方式你知道嗎?編輯:這工作得很好,如果我可以upvote你! –

+0

將'catch'塊留空並希望'異常'將自動處理* *可能聽起來不那麼乏味*但不是應該做什麼。 –

+0

哦,我甚至沒有談論catch塊,只是實例化一個新的字符串,然後使用parseInt()方法。您仍然可以將catch塊留空而沒有任何影響! –

0

你需要做的是確保只有您的掃描儀是關閉的,而不是System.in。在這種情況下,調用scanner.close()會依次調用您在構造函數(System.in)中提供的源的close。你有兩個選擇。更優雅的一個辦法是使用 http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/input/CloseShieldInputStream.html

Scanner num = new Scanner(new CloseShieldInputStream(System.in)); 

一個純Java的解決方案將是

public class Scan { 
private static final class MyShieldedSystemIn extends InputStream { 
    @Override 
    public int read(byte[] b) throws IOException { 
     return System.in.read(b); 
    } 

    @Override 
    public int read(byte[] b, int off, int len) throws IOException { 
     return System.in.read(b, off, len); 
    } 

    @Override 
    public void close() throws IOException { 
     // do nothing 
     System.out.println("ignoring call to close()"); 
    } 

    @Override 
    public int read() throws IOException { 
     return System.in.read(); 
    } 
} 
final static InputStream SSI = new MyShieldedSystemIn(); 
public static void main(String[] args){ 
    int numCom = 0, count = 0; 
    boolean valid = false; 


    do{ 
     Scanner num = new Scanner(SSI); 
     try{ 
      if(count == 0){ 
       System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
       count++; 
      }else{ 
       System.out.println("Error!\nPlease enter a number between 1-8."); 
      } 
      numCom = num.nextInt(); 
     }catch(Exception e){} 
     if(numCom < 9 && numCom > 0){ 
      valid = true; 
     } 
     num.close(); 
    }while(!valid); 
}//main() 

}

+0

這是一個堅實的提示,但因爲它不幸在java中沒有對應,它只是更多地進入我的代碼在那天。 –

+0

老實說,只是爲了增加一件事,這也是對你的男人(或女人,idk)這麼好的最有見地的帖子! :) –

+0

這是很多不必要的代碼。您不需要***關閉掃描儀。 – Boann

0

當您關閉Scanner,它將關閉其輸入源,這在這種情況是System.in。一旦您關閉System.in,您將無法再閱讀輸入內容。不要關閉Scanner。它沒有任何需要關閉的資源,除非你想關閉輸入源,在這種情況下你不需要關閉輸入源。如果您刪除num.close();呼叫,您的代碼將正常工作。

編輯:仍然,你正在爲此工作繁重。您的代碼的長度可以減半無損失的功能:

int numAdversaries; 
System.out.println("Okay, how many adversaries would you like to be put up against? (between 1-8)"); 
for (;;) { 
    try { 
     numAdversaries = new Scanner(System.in).nextInt(); 
     if (numAdversaries >= 1 && numAdversaries <= 8) break; 
    } catch (Exception e) {} 
    System.out.println("Error!\nPlease enter a number between 1-8."); 
}