0

So when I run my program's main method it prints:

Enter number of test cases: 
1
Enter string 1

Enter string 2
rat apple cat ear cat apple rat

For some reason it prints Enter string 1 and Enter string 2 before I even put in anything for String one. Can anyone shed any light as to why this is happening. Is there something wrong with the way I have the BufferReader setup?

Code:

public static void main(String[] args) throws IOException
        {    
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter number of test cases: ");
            int testcases = in.read();

            System.out.println("Enter string 1");
            String[] str1 = in.readLine().split(" ");

            System.out.println("\nEnter string 2");
            String[] str2 = in.readLine().split(" ");

            for(int i = 0; i < testcases; i++)
            {
                String result = lcss(str1, str2);
                System.out.println("\nLCSS: "+ result);
                System.out.println("\nLCSS Length = "+ result.length());
            }

        }
Beginner
  • 143
  • 4
  • 11
  • Use `Integer.parseInt(in.readLine())` for reading testcases. – Uma Kanth Nov 13 '15 at 05:19
  • Do not use read() function for reading input from the console as the function return **-1** when the end of stream has been reached mostly applicable for reading from a file. Use readLine() for better functionality, and parse integer value from that. – Deep LF Nov 13 '15 at 07:16

3 Answers3

0

Please use the following.

 int testcases = Integer.valueOf(in.readLine());

Read more on BufferedReader.read()

R.G
  • 3,885
  • 2
  • 12
  • 20
0

EDIT:

  1. Get the number of test cases as integer

  2. Create a 2 dimensional array to store test cases. First dimension holds each test case & second dimension holds String[] of word list in each test case.

  3. Iterate through "for loop" until you get each test case string array for total number of test cases,

Sample code:

public static void main(String[] args) throws Exception
{    
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of test cases: ");
    int testcases = in.nextInt();
    System.out.println("test cases:"+testcases);

    String[][] strs = new String[testcases][];
    for ( int i =0; i< testcases ; i++ ){
        System.out.println("Enter string:"+i);
        in = new Scanner(System.in);
        if (in.hasNext()) {
            String s = in.nextLine();
            System.out.println(s);
            strs[i] = s.split(" ");
            System.out.println("Length:"+strs[i].length);
        }
        System.out.println();

    }

    // Add your logic
}
Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
0

int testcases = in.read(); doesn't read the line break (when you press Enter).

The readLine() in the line String[] str1 = in.readLine().split(" "); will now begin to read directly after the number you entered and search for the next line break. The line break from you entering the number is now found and the function directly returns without waiting for your input.

So much for the explanation on what causes your program to behave the way it does.

Now you do have another error, as BufferedReader.read() doesn't do what you think it does. Check the documentation

So when you enter a 1 your testcases varaible will contain the UTF-16 value of the character '1' which is 31.

As other answers already pointed out you should either use Integer.valueOf(in.readLine()); to get the value for testcases or use Scanner

Simon Kraemer
  • 5,013
  • 1
  • 16
  • 43