2012-02-08 3 views
4

自分のプログラムに、ファイルの一部を読み込んでHTML用にフォーマットするスキャナがあります。ファイルを読んでいるときに、スキャナが行末にあることをスキャナに知らせる方法を知り、次の行に書き始める必要があります。ここでスキャナで行の終わりを判断するのに助けが必要

は私のコードの関連部分で、私は何を残した場合、私に知らせて:

//scanner object to read the input file 
    Scanner sc = new Scanner(file); 

    //filewriter object for writing to the output file 
    FileWriter fWrite = new FileWriter(outFile); 

    //Reads in the input file 1 word at a time and decides how to 
    ////add it to the output file 
    while (sc.hasNext() == true) 
    { 
     String tempString = sc.next(); 
     if (colorMap.containsKey(tempString) == true) 
     { 
      String word = tempString; 
      String color = colorMap.get(word); 
      String codeOut = colorize(word, color); 
      fWrite.write(codeOut + " "); 
     } 
     else 
     { 
      fWrite.write(tempString + " "); 
     } 
    } 

    //closes the files 
    reader.close(); 
    fWrite.close(); 
    sc.close(); 

[OK]を、私は)(sc.nextLine知った;,私はまだ知りません私がいつ行末にいるかを判断する方法。

+0

ヒント:あなたの投稿のタイトルを特定の質問にしてください。 – nes1983

+3

最後にsc.hasNext()がfalseになると、そうですか? – kosa

+0

あなたは 'FileWriter'を別の行に書き始める方法を知りたくありませんか?あなたの記事から、Scannerに必要な機能が備わっているので、そのようにすることができます。 –

答えて

5

スキャナのみを使用する場合は、データグリッドのnextLine()にインスタンシエートして(スキップした行のみを返すように)、tempをスキャンする新しいScannerオブジェクトを作成する必要があります文字列。このようにして、あなたはその行だけを使用しており、hasNext()は偽陽性を返しません(実際には偽陽性ではありません。最初のスキャナをnextLine()に置き、テンポラリ文字列を変更して、新しい行をスキャンするように2番目のスキャナを変更するだけです。

1

うわー私は10年間javaを使用してきましたが、スキャナーのことは聞いたことがありません! デフォルトでは空白区切り文字が使用されているように見えるので、行末がいつ発生するかはわかりません。あなたはスキャナの区切り文字を変更することができるように

が見える - Scanner Classで例を参照してください。あなたはそれをチェックする必要がある場合

String input = "1 fish 2 fish red fish blue fish"; 
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*"); 
System.out.println(s.nextInt()); 
System.out.println(s.nextInt()); 
System.out.println(s.next()); 
System.out.println(s.next()); 
s.close(); 
1

を行は通常そう\nまたは\rによってdelimittedされている、あなたはそれをそれをやってみることができますあなたはすでにnextLine()を使用しているので、なぜあなたがしたいと思うのか分かりませんが、全体の行を読むことができます。

hasNext()があなたの特定のケースでは機能しないと心配している場合は、Scanner.hasNextLine()があります(理由はわかりません)。

1

あなたはここにhasNextLineとスプリットを使用して、同じコードがあり、言葉によって代わりに単語を行毎にファイルを反復処理する方法のhasNextLineを使用し、その後空白で行を分割し、単語

であなたの操作を行うことができます

//scanner object to read the input file 
Scanner sc = new Scanner(file); 

//filewriter object for writing to the output file 
FileWriter fWrite = new FileWriter(outFile); 

//get the line separator for the current platform 
String newLine = System.getProperty("line.separator"); 

//Reads in the input file 1 word at a time and decides how to 
////add it to the output file 
while (sc.hasNextLine()) 
{ 
    // split the line by whitespaces [ \t\n\x0B\f\r] 
    String[] words = sc.nextLine().split("\\s"); 
    for(String word : words) 
    { 
     if (colorMap.containsKey(word)) 
     { 
      String color = colorMap.get(word); 
      String codeOut = colorize(word, color); 
      fWrite.write(codeOut + " "); 
     } 
     else 
     { 
      fWrite.write(word + " "); 
     } 
    } 
    fWrite.write(newLine); 
} 

//closes the files 
reader.close(); 
fWrite.close(); 
sc.close(); 
+0

おかげで、これは本当に役に立ちました!! –

関連する問題