2011-12-03 82 views
0

任務:從輸入文件讀取一行。如果該行的第一個單詞是PRINT,則打印該行其餘部分的內容。每次迭代從輸入文件讀取一行

代碼:

else if(Data.compareTo("PRINT") == 0){ 
    while(inFile.hasNext()){ 
     Data = inFile.next(); 
     System.out.print(Data + " "); 
    } 
} 

問題:使掃描儀一次只能讀取一行信息如何代碼掃描儀?

+0

您應該爲您的代碼寫入哪種語言添加標籤 – TJD

+0

'inFile'的類型是什麼? – jprofitt

回答

0
public static void ReadAndProcessPrint(File fileToRead) throws FileNotFoundException { 
    java.util.Scanner scanner = new Scanner(fileToRead); 
    while(scanner.hasNextLine()){ 
     String line = scanner.nextLine(); 
     if(line.startsWith("PRINT")){ 
      String restOfLine = line.substring(5); 
      System.out.println(restOfLine); 
     }else{ 
      //do other things 
     } 
    } 
}