2013-12-12 74 views
-1

这是我的课:扫描仪有一个无限循环

public class class1{ 
    public static void main(String[] args) {    
     File source = new File("E:\\NUS_WID_Tags\\All_Tags.txt"); 
     File target = new File("fiche1Filtered3.txt"); 
     int i=0; 

     try { 
      Scanner s = new Scanner(source); 
      PrintStream psStream= new PrintStream(target); 
      while (s.hasNext()) {     
       System.out.println(i++); 
      }     
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

程序进入无限循环。

回答

6

你忘了消耗实际的输入。 hasNext doesn't consume the input

扫描仪不会超过任何输入。

在循环中插入一个电话next()

while (s.hasNext()) { 
    String str = s.next(); 
    System.out.println(i++); 
}