Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# Conversion from ASCII to integer.

Vishnu Sharma
Ranch Hand
Posts: 55
Is there any method or way in java by which we can convert ASCII value to its integer or char value. As the output of System.in.read() method, we are getting the ASCII values instead of integer. Please reply, if there is any solution for this or we need to write a seperate programe for conversion??

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15636
46
What do you mean by "ASCII value instead of integer"? The ASCII code of a character is just a number.

Rob Spoor
Sheriff
Posts: 20751
68
I think that Vishnu is getting 48 but wants 0. The solution is easy:
1) subtract '0' Yes that's right, you can use chars in mathematical expressions.

2) even better, use Character.digit(x, 10). This has better error checking; it returns -1 if the read character is not numeric, instead of, well, anything.

Vishnu Sharma
Ranch Hand
Posts: 55
Rob Prime wrote:I think that Vishnu is getting 48 but wants 0. The solution is easy:
1) subtract '0' Yes that's right, you can use chars in mathematical expressions.

2) even better, use Character.digit(x, 10). This has better error checking; it returns -1 if the read character is not numeric, instead of, well, anything.

Yeah you are right, i want integer value for condition check, but getting ASCII value. Here is my code:
public class Loops {
public static void main(String args[])throws java.io.IOException {
int k;
System.out.println("Please enter any integer : ");
System.out.println(k);
while(k<10){
System.out.println("Hello Java");
k=k+1;
}
System.out.println("You are out of all loops");
}
}

Here if i am giving k =4, i am getting its ASCII value that is 52. Due to this, While loop is not getting executed, as 52 is also an integer value.
Now suggest me the solution..

Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.

Campbell Ritchie
Sheriff
Posts: 51423
87
Why are you using System.in.read() in the first place?

Rob Spoor
Sheriff
Posts: 20751
68
Sridhar Santhanakrishnan wrote:In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.

You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.

Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
Rob Prime wrote:
You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.

Yes, I was. But I thought the OP was not clear with using chars. He asked for the "solution" again.

Vishnu Sharma
Ranch Hand
Posts: 55
Campbell Ritchie wrote:Why are you using System.in.read() in the first place?

I want to get input from user, thats why using System.in.read(). I am a beginner in java. Is there any other method, by which we can get values entered by user??

Vishnu Sharma
Ranch Hand
Posts: 55
Sridhar Santhanakrishnan wrote:
Rob Prime wrote:
You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.

Yes, I was. But I thought the OP was not clear with using chars. He asked for the "solution" again.

Thats fine. But it is an alternate only, that use ASCII values in condition itself. So, there is no method exists by which we can convert output of System.in.read() method to integer from ASCII.

Rob Spoor
Sheriff
Posts: 20751
68

Vishnu Sharma
Ranch Hand
Posts: 55
Rob Prime wrote:You apparently choose to ignore my posts, since I have given you an answer already in the second reply to this thread. But if you don't want to read it, go ahead. It's your problem, not mine.

I did not ignore your posts buddy. But as per your quote i understood that use the ASCII values in condition itself. right??

Campbell Ritchie
Sheriff
Posts: 51423
87

Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
Vishnu Sharma wrote:
Sridhar Santhanakrishnan wrote:
Rob Prime wrote:
You mean between '0' and '9', don't you? Yes they are the same values, but using chars makes it easier to read. For me, 48 doesn't mean a thing, whereas '0' does.

Yes, I was. But I thought the OP was not clear with using chars. He asked for the "solution" again.

Thats fine. But it is an alternate only, that use ASCII values in condition itself. So, there is no method exists by which we can convert output of System.in.read() method to integer from ASCII.

Again, if you can read Rob's post, he has mentioned a "method" that would convert ASCII to Integer.

Character.digit(x, 10) - returns the integer if x is a valid number i.e between 48 and 57
-1 otherwise

So you would always "get an integer" using the digit() method. Hope this helps.

Vishnu Sharma
Ranch Hand
Posts: 55