我有一个文本文件:
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时,它将Filename设为apple.jpg,这是正确的,但Name和Brief是番茄的。我怎么能纠正这个? 提前谢谢。
答案 0 :(得分: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);
}
}
答案 1 :(得分:1)
如果要在拍摄时打印所有三个项目,则需要将打印内容放入循环中。决定何时打印的简单方法是检查所有三个项目是否为空。以下是如何做到这一点:
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 = "";
}
}
答案 2 :(得分:1)
你在while循环之外有你的printlns。因此,只有在读完整个文件后才会打印数据。
包含Name和Brief的最后两行显然是“Tomato”,因此它打印它们,你通过检查apple来限制你的文件名,因此不会被接下来的两个文件名行替换。
答案 3 :(得分:0)
你在while循环中的逻辑是错误的
提示
在while
循环内
读取一行
检查行是否以“Filename:”开头,如果它包含apple。现在它将filename
设置为filename = line.substring(10, line.length());
。
由于else if
为真,因此不会输入if
条件。
你看到了问题吗?
答案 4 :(得分:0)
问题在于您的情况。
line.startsWith("Filename:") && (line.contains("apple")
在上面的行中,当且仅当它是apple时,才能获取水果的文件名。如果没有,你只需继续获得另外两行。
结果是,当你到达橙色和番茄时,你跳过阅读他们的文件名,只读他们的名字和简短。您需要更改有关如何检索子字符串的逻辑。
要提取子字符串,请不要硬编码子字符串开头和结尾的位置。相反,使用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);
}
}
}