2016-04-05 41 views
1

任何人都可能能夠告訴我爲什麼此代碼不打印輸出?我沒有收到任何錯誤,但它只是不打印。它是從.txt文件讀取的(下面的代碼在下面)。從文件中讀取不是打印到輸出

代碼:於明年其他

public class ReadFromFile { 

    public static void main(String[] args) throws FileNotFoundException { 
     File file = new File("CarInfo.txt"); 

     try (Scanner sc = new Scanner(file)) { 
      while (sc.hasNext()) { 
       String carTab = sc.next(); 
       // Looking for tag 'Station:' 
       if (!carTab.equals("Car:")) continue; 

       if (!sc.hasNext()) { 
        break; 
       } 

       Car = sc.next(); 
       if (!sc.hasNextInt()) { 
        continue; 
       } 
       int x = sc.nextInt(); 
       if (!sc.hasNextInt()) { 
        continue; 
       } 
       int y = sc.nextInt(); 
       System.out.println(car + " " + x + " " + y); 
      } 

     } catch (FileNotFoundException e) { 
      System.out.println("File not found"); 
     } 
    }  
} 
+3

設置一個斷點後,它不會消耗任何東西,通過您的代碼步。這比讓我們猜測什麼是錯誤要容易得多。 – f1sh

+1

我測試了您的代碼沒關係 – Abdelhak

+0

@Abdelhak是否爲您打印? – BlaBla

回答

0

使用nextLine()()。 nextLine()可以消耗回車

即使先前對hasNext()的調用返回true,next()也可能會在等待輸入進行掃描時阻塞。每次調用nextInt()的時間

,只讀取次數和數量

相關問題