-2

Why do I get an exception when parsing the characters to integer using the read() method instead of readLine() of BufferedReader. I tried using this code below:

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a= Integer.parseInt(br.read());

error was : cannot convert int to string.. very strange error

Philipp
  • 4,645
  • 3
  • 47
  • 80
Isha
  • 27
  • 1
  • 6
  • try with this `Integer.toString(integer)` FYI http://stackoverflow.com/questions/8973381/why-cannot-cast-integer-to-string-in-java – Subodh Joshi Aug 05 '15 at 06:10
  • No i do not want to convert integer to string. that is why i wrote strange errror.. as i want to convert character into integer using read method – Isha Aug 05 '15 at 06:14

4 Answers4

1

You need to parse String in Integer.parseInt("need a String").

But here you are providing int. br.read() return an int

You can use

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a= br.read();

Edit:

Yes, when you enter 5, br.read() read is as 53, since unicode value of '5' is 53.

So you want to deal with int. you can try int a=Integer.parseInt(br.readLine());

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • I want to parse the number like 4 or 5 or any integer value which is is read as character through read() method – Isha Aug 05 '15 at 06:11
  • @Isha yes. You can try this way. – Ruchira Gayan Ranaweera Aug 05 '15 at 06:13
  • When you say "or any integer value" you mean "any single digit" right? Because `read()` only reads a single character... how would you expect to read the number 1234 that way? – Jon Skeet Aug 05 '15 at 06:14
  • @JonSkeet yes here m talking about single digit number only. N i had already tried the way you had told me, but in output some other no. is coming ( i guess its the ASCII value). For. eg. if i input 5 , then when i am printing that number, some other number is coming. – Isha Aug 05 '15 at 06:20
  • @Isha: Then you're not doing what's in my answer (not this one), as what's in my answer works. But we can't see what you're doing, because you haven't provided the code you've tried, which makes it really hard to help you... – Jon Skeet Aug 05 '15 at 06:25
  • @Isha yes.. That is also known.... you should aware that... see my edit... when you enter 5, br.read() read is as 53, since unicode value of '5' is 53 – Ruchira Gayan Ranaweera Aug 05 '15 at 06:26
  • @JonSkeet i have provided the lines of code where the problem was coming. Again have a look. And yes i have not tried your way completely but somewhat similar to it was done by me – Isha Aug 05 '15 at 06:28
  • @RuchiraGayanRanaweera sir i am aware that i can do this with readLine(). Question was about read(). :) – Isha Aug 05 '15 at 06:35
  • @Isha Yes.. as I explain here you will not able to do what you trying with read(). you need to read Java doc as well. http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#read() – Ruchira Gayan Ranaweera Aug 05 '15 at 06:38
1

Reader.read() just returns a single character - but as an int, so you can tell if you've reached the end of the data.

Now there isn't any Integer.parseInt(int) method, which is why the calling is failing - there isn't even Integer.parseInt(char). You could convert to char and then call Character.getNumericValue(char)... or you could just use:

int digit = br.read() - '0';

... and then check that digit is in the range 0-9.

Alternatively you could create a string from the single character you've read and call Integer.parseInt, but that seems like overkill...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • can u please tell what basically this '0' is doing in your code : (((int digit = br.read() - '0';)))) – Isha Aug 05 '15 at 08:01
  • 1
    @Isha: That's subtracting the UTF-16 code unit for the digit 0... basically the integer you get is the UTF-16 code unit for the character in the input, and you want to convert '0' to an integer 0, '1' to an integer 1 etc... as the code units for '0' to '9' are contiguous, you can just subtract '0'. – Jon Skeet Aug 05 '15 at 08:03
0

Use readLine() function. In this case value is read as a string.

 int a= Integer.parseInt(br.readLine());

The read() method returns a character as an integer.parseInt() expects a string but you are giving an integer. So here the code int a= Integer.parseInt(br.read()) tries to convert int to int. Hence the error.

Deepu
  • 7,592
  • 4
  • 25
  • 47
  • Yes m aware that i can do this using readLine(). But the question why we can't do it with read() – Isha Aug 05 '15 at 06:13
0

Reader and its subclasses contains the int read() method, which reads a single character as an int.

The reason it returns an int instead of char is because it must be able to return -1 when end of stream has been reached (as well as the 65536 characters that char provides).

If you want to read a single character, you can just check that the value wasn't -1 and then cast it to char. If you want to treat the character as an int value, you don't need to do anything.

Kayaman
  • 72,141
  • 5
  • 83
  • 121