私はテキストファイルを持っています:
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になりますが、これは正しいですが、名前とブリーフはトマトのものです。どうすればこれを修正できますか? 前もって感謝します。