2011-12-03 8 views
0

タスク:入力ファイルから行を読み込みます。行の最初の単語がPRINTの場合は、残りの行の内容を出力します。繰り返しごとに入力ファイルから1行を読み込みます。

コード:

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

が質問:スキャナは一度に1行の情報を読み取るようにスキャナをコーディングする方法

+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 
     } 
    } 
} 
関連する問題