Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法在文本文件中查找字符串,然后在未找到字符串时将字符串添加到文本文件中,否则拒绝输入_Java_Text_Methods - Fatal编程技术网

Java 方法在文本文件中查找字符串,然后在未找到字符串时将字符串添加到文本文件中,否则拒绝输入

Java 方法在文本文件中查找字符串,然后在未找到字符串时将字符串添加到文本文件中,否则拒绝输入,java,text,methods,Java,Text,Methods,我想让程序在文本文件fruit.txt中搜索 检查里面是否已经有水果 如果存在水果,则提示用户输入另一个1 否则 添加到文本文件的下一行 这就是我目前得到的。 但我不知道为什么它不是我想要的 在我的文本文件中,它从3个水果开始 public static void main(String[] args) throws IOException{ if (args.length == 1) { BufferedReader bf = new BufferedReader (new FileR

我想让程序在文本文件fruit.txt中搜索 检查里面是否已经有水果

如果存在水果,则提示用户输入另一个1

否则 添加到文本文件的下一行

这就是我目前得到的。 但我不知道为什么它不是我想要的

在我的文本文件中,它从3个水果开始

public static void main(String[] args) throws IOException{
if (args.length == 1)
{
    BufferedReader bf = new BufferedReader (new FileReader("fruit.txt"));
    int linecount = 0;
    String line;
    //run thur the txt file to check if input exist
    while (( line = bf.readLine()) != null)
    {
        linecount++;
        int indexfound = line.indexOf(args[0]);
        if (indexfound > -1) {
            System.out.println("fruit exist on line " + linecount);
            System.out.println("add another fruit");    
            System.exit(0);
        } else {
            BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
            String fruit = "";
            fruit = args[0];
            bw.write("\r\n" + fruit);
            System.out.println(fruit+ "added"); 
        }
    }
    f.close();
    bw.close();
}
在我加入浆果之后

apple
orange
pear
在我加入甜瓜之后

apple
orange
pear
berry
berry

您只是在检查第一行中的水果,如果没有找到,您将继续添加它

你需要先完整地阅读你的文件,检查每一行,它是否包含你的水果,如果它不包含,就把水果倒进去。如果它包含,则拒绝它

所以,在这段时间里,你需要把另一部分移到外面。当找到水果时,您可以将布尔变量设置为true,然后根据布尔变量的值决定是否添加水果,而不是执行
System.exit()

apple
orange
pear
berry
berry
melon
melon
melon
melon

请在发布前正确格式化代码。@newbieprogrammer。是的。试试上面的代码。请注意,我正在读取while循环中的每一行,如果一行包含结果,我将布尔变量设置为true,然后中断循环。@newbieprogrammer。编辑了代码。。如果(!found),则应为
。检查一下。我在找国旗指示器,现在我看到了answer@KaipaMSarma. 嗯。在这种情况下,总是需要Yup标志指示器。:)@罗希特·贾因非常感谢你的休息;比使用系统退出更好
boolean found = false;
while (( line = bf.readLine()) != null) {

    linecount++;
    int indexfound = line.indexOf(args[0]);
    if (indexfound > -1) {
        System.out.println("fruit exist on line " + linecount);
        System.out.println("add another fruit");   
        found = true;
        break;
    }
}

if (!found) {

    BufferedWriter bw = new BufferedWriter(new FileWriter("fruit.txt", true));
    String fruit = "";
    fruit = args[0];

    bw.write("\r\n" + fruit);
    System.out.println(fruit+ "added"); 

    bw.close();  // You need to close it here only. 
}

bf.close();