Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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 使用BufferedReader从文本文件中读取段落_Java - Fatal编程技术网

Java 使用BufferedReader从文本文件中读取段落

Java 使用BufferedReader从文本文件中读取段落,java,Java,我正在尝试向用户提供他们的每日星座。我有一个大的文本文件,看起来有点像这样: 白羊座 当你在[继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][结束]的时候,从来没有一个沉闷的时刻 金牛座 进入[继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][结束] 此文本文件包含所有黄道带

我正在尝试向用户提供他们的每日星座。我有一个大的文本文件,看起来有点像这样:

白羊座

当你在[继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][结束]的时候,从来没有一个沉闷的时刻

金牛座

进入[继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][继续占星术][结束]

此文本文件包含所有黄道带星座的所有星座。我试图根据用户的输入打印出正确的段落。有人能帮我吗?这就是我到目前为止所做的:

public static void getDailyHoroscope() throws IOException{
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input your zodiac sign:");
    String sign = sc.nextLine();
    String file = ".txt file";

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            System.out.print(line);
        }
        br.close( );
    }
    catch(FileNotFoundException e) {
        System.out.println("Cannot find" + file);
}

我不知道如何进行。这只会在控制台中吐出整个文本文件。我想根据用户的符号将输出按段落分开。

看起来您做了很多工作


在您的情况下,最好先阅读整个文件,将其分段,存储到某个集合中,如
Map
Set
,然后在该集合中搜索所需的条目。

我的建议是继续阅读该文件,直到您点击用户的输入,然后是空行,然后继续阅读,直到你找到另一行,然后是一个空行

类似的方法可能会奏效:

public static void getDailyHoroscope() throws IOException{
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input your zodiac sign:");
    String sign = sc.nextLine();
    String file = ".txt file";

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        boolean print = false;
        while ((line = br.readLine()) != null) {
            //If our print flag is false and we found a line that's just our users's sign
            if((!print) && line.trim().equalsIgnoreCase(sign)){
                //set our print flag to true
                print = true;
            }
            if(print){
                //if we're printing, and the line we just read was completley empty, unset our print flag.
                if(line.trim().isEmpty()){
                    print = false;
                }
                System.out.println(line);
            }
        }
        br.close( );
    }catch(FileNotFoundException e) {
        System.out.println("Cannot find" + file);
    }
}
记住我在记事本上写的,可能不起作用

考虑到您的上述职位,请考虑以下几点: 在这个代码的当前状态下,如果它能工作,它可能只打印星座名称,然后点击一个空行并停止打印,您需要考虑这种可能性

public static void getDailyHoroscope() throws IOException{
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input your zodiac sign:");
    String sign = sc.nextLine();
    String file = "C:/Users/Laptop/Downloads/horoscope.txt";
    sc.close();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        //include boolean exist sign, only because message
        boolean existSign = false;

        while ((line = br.readLine()) != null) {
            if (line.equalsIgnoreCase(sign)) {
                //skip first empty line
                br.readLine();
                line = br.readLine();
                while (line!=null && !(line.isEmpty())) {
                    System.out.println("\n"+line);
                    line = br.readLine();
                }

                existSign = true;
                break;
            }
        }
        if (!existSign)
            System.out.println("Please input zodiac sign which exists");

        br.close();
    } catch (FileNotFoundException e) {
        System.out.println("Cannot find" + file);
    }
}
在外部while循环中读取行,而该行不等于符号。 若读入一行,则跳过空行,然后读入有用的文本行。只要它到达末尾或下一个空行,打印该行并阅读下一行。在内部while之后,将existSign设置为true并中断外部while。

尝试:

Scanner sc = new Scanner(System.in);
        System.out.println("Please input your zodiac sign:");
        String sign = sc.nextLine();
        String file = ".txt file";

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            String sign2 = "";
            String content = "";
            String nl = System.getProperty("line.separator");
            //I assummed that your text not end with [end] for each sign zodiac.
            while ((line = br.readLine()) != null) {
                String l = line.trim().toLowerCase();
                //Check if line is equal than some zodiac sign.
                String sign3 = "";
                if (l.equals("aries")) {
                    sign3 = "aries";
                } else if (l.equals("tauro")) {
                    sign3 = "tauro";
                } //complete here all zodiac sing with else if.

                //Check if sign is equal than sign enter by user and data content for this sign still not ready. 
                if (sign2.length() == 0 && sign3.length() != 0 && sign.toLowerCase().equals(sign3)) {
                    sign2 = sign3;
                } else {
                    //Check if we need concatenate data for this sign.
                    if (sign2.length() > 0 && sign3.length() == 0) {
                        content += line + nl;
                    } else {
                        //If this line is equal other sign and content data we finalized and break.
                        if (sign2.length() > 0 && sign3.length() != 0) {
                            break;
                        }
                    }
                }

            }
            br.close();

            System.out.println(content);
        } catch (Exception ex) {
            System.out.println("Cannot find" + file);
        }

一个文件包含所有的生肖名字?白羊座,金牛座……你到底试过什么?这是读取和打印文本文件的代码。关于如何只打印出所需的部分,您有什么想法?请在异常处理程序上固定右括号。同样,考虑使用资源尝试:<代码>尝试(BuffReDebug阅读器BR =新BuffErdRe读器(新FieleDead(文件))){ /代码>。我无法想象OP段落中的每一个都以“代码> [Endo]结尾。< /代码>,就像我怀疑它们都有<代码>的内容[继续占星术]一样。请解释为什么这是有用的。虽然代码帮助很大,但理解通常需要的不仅仅是这样。只有代码的答案很少是如此可取的。请考虑为什么它应该为OP版本1.2解释一个解释——检查空字符串感谢建议,这是我的第一个答案。只有代码的答案很少如此令人满意。ider添加了一个解释,解释为什么它应该适用于OP。