• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在文本文件查找字符串,然后在未找到字符串的情况下字符串添加到文本文件的方法,否则拒绝输入

用户头像
it1352
帮助1

问题说明

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

I want to have the program to search in a text file fruit.txt check if a fruit already exist in it.

如果存在水果,则提示用户再输入1

if fruit exist then prompt user to input another 1

else add in to the nextline of the text file

这是我到目前为止所得到的. 但我不确定为什么不是我想要的.

This is what I got so far. but I am not sure why it isnt what I wanted.

in my text file it started with 3 fruit

apple
orange
pear

添加浆果后

apple
orange
pear
berry
berry

我加瓜后

apple
orange
pear
berry
berry
melon
melon
melon
melon

正确答案

#1

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

You are just checking for the fruit in the first line, and if it is not found, you are continuing to add it.

您需要首先完整地读取文件,以检查每一行是否包含您的水果,然后,如果其中不包含水果,则只需将其转储到其中即可.如果包含,则拒绝它.

You need to read your file completely first, one for checking each line, that it contains your fruit or not, and then if it does not contain, just dump that fruit in it. And if it contains, reject it.

因此,您需要将其他部分移到外面.而不是在找到水果时执行System.exit(),而是可以将布尔变量设置为true,然后根据布尔变量的值来决定是否添加水果.

So, in your while, you need to move that else part outside. And rather than doing System.exit() when fruit is found, you can set a boolean variable to true, and then later on, based on the value of boolean variable, you can decide whether to add the fruit or not.

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjjgc
系列文章
更多 icon
同类精品
更多 icon
继续加载