Search code examples
javaiobufferedreaderbufferedinputstream

Java BufferedReader - the last line of output isn't read automatically, it reads only when i press enter


This is my code:

public class hack 
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader sc = new  BufferedReader(new InputStreamReader(System.in));
        int count = 0;
        int cases = Integer.parseInt(sc.readLine());
        String pie = "31415926535897932384626433833";
        String str;
        for(int i = 0; i < cases; i++)
        {
            str = sc.readLine();
            String []strarr = str.split(" ");
            String str1 = "";
            for(int k = 0; k < strarr.length; k++)
            {
                char check[] = strarr[k].toCharArray();
                count = 0;
                for(int j = 0; j < strarr[k].length(); j++)
                {
                    if((check[j] > 64 && check[j] < 91) || (check[j] > 96 && check[j] < 122))
                    {
                        count++;
                    }
                }
                str1 = str1 + count;
            }
            if(str1.equalsIgnoreCase(pie.substring(0,strarr.length)))
            {
                System.out.println("It's a pi song.");
            }
            else
            {
               System.out.println("It's not a pi song.");
            }
        }
    }
}

The input file is as follows :

4<br>
dHjVPihCZ BjHi OkWbQBH NIQM Ue BPIJHS ZdWQLMIxy wzVGBhx GqQjKMeJ ZmfZo l qmDWBUrs<br>
YkoGQTJYN vgy tJ k B g YV T iXExLPfFk eL TTcOtic MRbmKE<br>
RMSIYuD GiRYQ U K THGUT XYycRULD pTG NqlHUO RFEG rJ sqcGVd jZ IhTlGUvE FLQfISMqN cW<br>
Rzm YPPGEQDXq PsDpNLCd qLTFiFDn wFqpGvSB YQqIZY sfWRwGJ<br><br>

The output is as follows :

dHjVPihCZ BjHi OkWbQBH NIQM Ue BPIJHS ZdWQLMIxy wzVGBhx GqQjKMeJ ZmfZo l qmDWBUrs<br>
YkoGQTJYN vgy tJ k B g YV T iXExLPfFk eL TTcOtic MRbmKE<br>
RMSIYuD GiRYQ U K THGUT XYycRULD pTG NqlHUO RFEG rJ sqcGVd jZ IhTlGUvE FLQfISMqN cW<br>
It's not a pi song.<br>
It's not a pi song.<br>
It's not a pi song.<br>
Rzm YPPGEQDXq PsDpNLCd qLTFiFDn wFqpGvSB YQqIZY sfWRwGJ<br>
it's not a pi song.
<br><br>

In this I have given 4 as no. of cases. As input I gave all four lines together, but it considered only first three lines as input and only their output is shown together, I want the output of all four lines to be shown together.


Solution

  • You need to have another line feed after your last input line. BufferedReader.readLine() javadoc:

    Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.