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
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 回答
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 = "";
    }
}
于 2013-11-06T03:09:00.877 回答
1

您的 printlns 在 while 循环之外。因此,只有在您阅读了整个文件后才会打印数据。

包含 Name 和 Brief 的最后两行显然是“Tomato”,因此它会打印它们,即您通过检查 apple 限制的文件名,因此不会被接下来的两行文件名替换。

于 2013-11-06T03:09:20.773 回答
0

您在 while 循环中的逻辑是错误的

暗示

while循环内

读取一行
,检查行是否以“文件名:”开头,是否包含苹果。现在它设置filenamefilename = line.substring(10, line.length());. 它不输入else if条件,因为它if是真的。

你看到问题了吗?

于 2013-11-06T03:05:15.040 回答
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);
   }

}
}
于 2013-11-06T03:05:58.333 回答