Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking inputs with BufferedReader in Java

Tags:

java

input

I have a bit of an annoying case here; wherein I am not able to take the input properly. I have always taken input via Scanner, and am not used to the BufferedReader.


INPUT FORMAT


First line contains T, which is an integer representing the number of test cases.
T cases follow. Each case consists of two lines.

First line has the string S. 
The second line contains two integers M, P separated by a space.

EXAMPLE

Input:
2
AbcDef
1 2
abcabc
1 1

My code so far:


public static void main (String[] args) throws java.lang.Exception
{
    BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
    int T= Integer.parseInt(inp.readLine());

    for(int i=0;i<T;i++) {
        String s= inp.readLine();
        int[] m= new int[2];
        m[0]=inp.read();
        m[1]=inp.read();

        // Checking whether I am taking the inputs correctly
        System.out.println(s);
        System.out.println(m[0]);
        System.out.println(m[1]);
    }
}

When inputting to the above example shown, I get the following output:

AbcDef
9
49
2
9
97
like image 759
OneMoreError Avatar asked Nov 16 '12 06:11

OneMoreError


People also ask

How do I get input from BufferedReader?

BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); Once we have created a BufferedReader we can use its method readLine() to read one line of characters at a time from the keyboard and store it as a String object. String inputString = input. readLine();

How do you take spaces in Java with inputs?

There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each value. Using nextInt( ) method of Scanner class.


2 Answers

BufferedReader#read reads single character[0 to 65535 (0x00-0xffff)] from the stream, so it is not possible to read single integer from stream.

            String s= inp.readLine();
            int[] m= new int[2];
            String[] s1 = inp.readLine().split(" ");
            m[0]=Integer.parseInt(s1[0]);
            m[1]=Integer.parseInt(s1[1]);

            // Checking whether I am taking the inputs correctly
            System.out.println(s);
            System.out.println(m[0]);
            System.out.println(m[1]);

You can check also Scanner vs. BufferedReader.

like image 148
Subhrajyoti Majumder Avatar answered Nov 14 '22 03:11

Subhrajyoti Majumder


The problem id because of inp.read(); method. Its return single character at a time and because you are storing it into int type of array so that is just storing ascii value of that.

What you can do simply

for(int i=0;i<T;i++) {
    String s= inp.readLine();
    String[] intValues = inp.readLine().split(" ");
    int[] m= new int[2];
    m[0]=Integer.parseInt(intValues[0]);
    m[1]=Integer.parseInt(intValues[1]);

    // Checking whether I am taking the inputs correctly
    System.out.println(s);
    System.out.println(m[0]);
    System.out.println(m[1]);
}
like image 26
Sumit Singh Avatar answered Nov 14 '22 03:11

Sumit Singh