【问题标题】:Loop a search for a string using BufferedReaders?使用 BufferedReaders 循环搜索字符串?
【发布时间】:2015-12-15 07:19:35
【问题描述】:

我正在使用两个 BufferedReader,一个用于读取文档,另一个用于从用户那里获取要搜索的字符串的输入,并听取了 here 的建议。到目前为止的代码如下:

import java.io.*;
import java.util.*;

public class Ejercicio6 {

    public static void main(String[] args) {

        Character answer = 'S'; 
        boolean exit = false;
        String name;
        String line;
        Scanner sc = new Scanner (System.in);
        boolean found = false;

        File file = new File ("/test/Ejercicio6/nombres.txt");

        try {

            do{

                exit = false;
                FileInputStream fis = new FileInputStream(file);
                BufferedReader readFile = new BufferedReader(new FileReader(file));
                BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Search a name, I'll tell you if it's found:");
                name = userInput.readLine();
                while ((line = readFile.readLine()) != null && found == false){
                    if(line.equals(name)) {
                        found = true;
                    }else
                        found = false;
                }

                if (found == true)
                    System.out.println("I have found the name " +name+ " in the file " +file.getName());
                if (found == false)
                        System.out.println("Can't find the name");
                fis.getChannel().position(0);
                fileRead = new BufferedReader(new InputStreamReader(fis));

                System.out.println("Do you want to try again? (Y/N)");
                answer = sc.nextLine().toUpperCase().charAt(0);
                if (answer =='S'){

                    exit = false;
                }else

                    exit = true;
                fileRead.close();
            }while (exit == false);

//      }catch (FileNotFoundException e) {
//          e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();

        }

    }

}

文档上有 3 个名字,但无论输入是否匹配,我总是得到“找到的名字”打印。正如here 所说,我试图弄清楚 getChannel() 和缓冲区是如何清除的,但我遇到了很多麻烦。我错过了什么?

【问题讨论】:

    标签: java bufferedreader filereader


    【解决方案1】:

    在打印匹配或不匹配的名称后,您需要再次将发现的标志取消设置为 false。 只需添加

    found = false;

    之前

    fis.getChannel().position(0);

    【讨论】:

    • 简单的解决方案,正如我所料。非常感谢,工作就像一个魅力:)
    【解决方案2】:
    if (line.equals(name)) {
        found = true;
    } else
        found = false;
    

    可以简化:

    found = line.equals(name);
    

    在while循环之前移动boolean found = false;

    package com.company;
    
    import java.io.*;
    import java.util.Scanner;
    
    public class Ejercicio6 {
        public static void main(String[] args) {
            Character answer;
            boolean exit;
            String name;
            String line;
            Scanner sc = new Scanner(System.in);
            File file = new File("file.txt");
            try {
                do {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedReader readFile = new BufferedReader(new FileReader(file));
                    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
                    System.out.println("Search a name, I'll tell you if it's found:");
                    name = userInput.readLine();
                    boolean found = false;
                    while ((line = readFile.readLine()) != null && found == false) {
                        found = line.equals(name);
                    }
                    if (found == true)
                        System.out.println("I have found the name " + name + " in the file " + file.getName());
                    if (found == false)
                        System.out.println("Can't find the name");
                    fis.getChannel().position(0);
                    BufferedReader fileRead = new BufferedReader(new InputStreamReader(fis));
                    System.out.println("Do you want to try again? (Y/N)");
                    answer = sc.nextLine().toUpperCase().charAt(0);
                    exit = answer != 'S';
                    fileRead.close();
                } while (exit == false);
    //      }catch (FileNotFoundException e) {
    //          e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 2023-03-17
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2016-10-17
      • 2020-11-01
      相关资源
      最近更新 更多