2011-12-03 48 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 
     } 
    } 
}