无法在Java上放置所需的名称

无法在Java上放置所需的名称,java,Java,我有一个文本文件: 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 s

我有一个文本文件:

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时,它将文件名设置为apple.jpg,这是正确的,但是Name和Brief是番茄的。我怎样才能纠正这个问题?
提前谢谢。

while循环中的逻辑是错误的

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);
    }

}
暗示

while
循环中

读一行
检查行是否以“Filename:”开头,以及是否包含apple。现在它将
filename
设置为
filename=line.substring(10,line.length())。
如果
if
为真,则不会输入
else条件


你看到问题了吗?

问题在于你的状况

line.startsWith(“文件名:”)和&(line.contains(“苹果”)

在上面的一行中,您获取水果的文件名,当且仅当它是apple时。如果不是,则继续获取其他两行

结果是,当你使用orange和tomato时,你会跳过读取它们的文件名,而只读取它们的名称和摘要。你需要改变检索子字符串的逻辑

要提取子字符串,不要硬编码子字符串的开始和结束位置。相反,使用
indexOf()
检索
的位置,并从下一个位置到结束获取子字符串


返回此字符串中第一次出现的 指定的字符。如果值为ch的字符出现在 由该字符串对象表示的字符序列,然后是索引 返回第一个这样的事件的值,即最小值 值k,以便:

 this.charAt(k) == ch
如果此字符串中没有此类字符,则为-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 = "";
    }
}

您的println位于while循环之外。因此,只有在读取整个文件后,才会打印数据


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

非常感谢大家!使用了David的建议,效果很好。给每个人一个+1作为感谢他们努力的一种方式。此程序仍然与原始程序有相同的错误。运行它并查看!@DavidWallace和bug是…?OP说“我遇到的问题是,当我设置apple时,它将文件名设置为apple.jpg,这是正确的,但名称和摘要是番茄的。我如何更正此问题?”您的程序仍然为番茄打印名称和摘要。运行它看看!这根本不是问题所在。
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);
   }

}
}
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 = "";
    }
}