1

私はテキストファイルを持っています:

Filename: apple.jpg
Name: Apple
Brief: Apple is a fruit

Filename: orange.jpg
Name: Orange
Brief: Orange is also a fruit

Filename: tomato.jpg
Name: Tomato
Brief: Tomato is not a fruit, it's a vegetable

私はコードをもっている:

public class Test
{
public static void main(String[] args) throws IOException{
    Scanner reader = new Scanner(new File("C:/textLocation.txt"));

    String filename = "";
    String name = "";
    String brief = "";

    String line = "";
    while (reader.hasNextLine()){
        line = reader.nextLine();

        if (line.startsWith("Filename:") && (line.contains("apple"))){
            filename = line.substring(10, line.length());
        } else if (line.startsWith("Name:")){
            name = line.substring(6, line.length());
        } else if (line.startsWith("Brief:")){
            brief = line.substring(7, line.length());
        }
    }
    System.out.println(filename);
    System.out.println(name);
    System.out.println(brief);
}
}

私が抱えている問題は、リンゴを設定すると、ファイル名がapple.jpgになりますが、これは正しいですが、名前とブリーフはトマトのものです。どうすればこれを修正できますか? 前もって感謝します。

4

5 に答える 5

1

取得した 3 つの項目すべてを印刷したい場合は、印刷をループ内に配置する必要があります。いつ印刷するかを決定する簡単な方法は、3 つの項目がすべて空でないことを確認することです。これを行う方法は次のとおりです。

while (reader.hasNextLine()){
    line = reader.nextLine();
    if (line.startsWith("Filename:") && (line.contains("apple"))){
        filename = line.substring(10); // line.length() parameter is optional
    } else if (line.startsWith("Name:")){
        name = line.substring(6);
    } else if (line.startsWith("Brief:")){
        brief = line.substring(7);
    }
    // Decide if you want to print or not: see if we've got all three
    if (filename.length() != 0 && name.length() != 0 && brief.length() != 0) {
        System.out.println(filename);
        System.out.println(name);
        System.out.println(brief);
        System.out.println("--------");
        // Reset for the next iteration
        filename = "";
        name = "";
        brief = "";
    }
}
于 2013-11-06T03:09:00.877 に答える
1

while ループの外に printlns があります。そのため、データはファイル全体を読み取った後にのみ印刷されます。

Name と Brief を含む最後の 2 行は明らかに "Tomato" であるため、それらを出力します。ファイル名は、apple をチェックして制限したため、次の 2 行のファイル名に置き換えられません。

于 2013-11-06T03:09:20.773 に答える
1
public class Test {
    public static void main(String[] args) throws IOException{
        Scanner reader = new Scanner(new File("C:/textLocation.txt"));

        String filename = "";
        String name = "";
        String brief = "";
        boolean lookingForName = false;
        boolean lookingForBrief = false;

        String line = "";
        while (reader.hasNextLine()){
            line = reader.nextLine();

            if (line.startsWith("Filename:") && (line.contains("apple"))){
                filename = line.substring(10, line.length());
                lookingForName = true;
                lookingForBrief = true;
            } else if (line.startsWith("Name:") && lookingForName){
                name = line.substring(6, line.length());
                lookingForName = false;
            } else if (line.startsWith("Brief:") && lookingForBrief){
                brief = line.substring(7, line.length());
                lookingForBrief = false;
            }
        }
        System.out.println(filename);
        System.out.println(name);
        System.out.println(brief);
    }

}
于 2013-11-06T03:04:52.413 に答える
0

問題はあなたの状態にあります。

line.startsWith("Filename:") && (line.contains("apple")
上記の行では、果物がリンゴである場合にのみ、果物のファイル名を取得します。そうでない場合は、そのまま他の 2 行を取得します。

その結果、オレンジとトマトにたどり着くと、ファイル名を読むのをスキップして、名前と概要だけを読むことになります。部分文字列を取得する方法について、ロジックを変更する必要があります。

部分文字列を抽出するには、部分文字列の開始位置と終了位置をハードコーディングしないでください。代わりに、次の位置から最後までの indexOf()位置を取得するために使用し、部分文字列を取得します。:


public int indexOf(int ch)  

指定された文字が最初に出現する、この文字列内のインデックスを返します。値 ch を持つ文字が、この String オブジェクトによって表される文字シーケンスに出現する場合、そのような最初の出現のインデックス、つまり、次のような最小値 k が返されます。

 this.charAt(k) == ch

本当です。この文字列にそのような文字がない場合は、-1 が返されます。


public class Test
{
public static void main(String[] args) throws IOException{
    Scanner reader = new Scanner(new File("C:/textLocation.txt"));

    String filename = "";
    String name = "";
    String brief = "";

    String line = "";
    while (reader.hasNextLine()){
        line = reader.nextLine();

        if (line.startsWith("Filename:") && (line.contains("apple"))){
            filename = line.substring(10, line.length());
        } else if (line.startsWith("Name:")){
            name = line.substring(6, line.length());
        } else if (line.startsWith("Brief:")){
            brief = line.substring(7, line.length());
        }
    }

   if(filename.equals("apple.jpg")){ // print only if you have apple. Else, ignore
        System.out.println(filename);
        System.out.println(name);
        System.out.println(brief);
   }

}
}
于 2013-11-06T03:05:58.333 に答える
0

while ループ内のロジックが間違っている

ヒント

whileループの内側


行が「ファイル名:」で始まるかどうか、およびリンゴが含まれているかどうかを確認する行を読み取ります。に設定filenameされfilename = line.substring(10, line.length());ます。else ifが真であるため、条件に入りませんif

問題がわかりましたか?

于 2013-11-06T03:05:15.040 に答える