BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please enter the size of array");
size = br.read();
sarray = new int[size];
for (int i = 0; i < size; i++) {
sarray[i] = i;
}
System.out.println(sarray.length);
When I tried to print the length of the array, it is showing as "51" even though i gave the size as "3".
Use readLine() method instead of read() method .
int size = Integer.parseInt(br.readLine());
read() method doesnt return the exact int value of input.
public int read() throws IOException Reads a single character. Overrides: read in class Reader Returns: The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached Throws: IOException - If an I/O error occurse
Ref : http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#read()