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??
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.
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 : ");
k= System.in.read();
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..
This message was edited 1 time. Last update was at by Vishnu Sharma
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
posted
0
In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.
This message was edited 1 time. Last update was at by Sridhar Santhanakrishnan
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25060
posted
0
Why are you using System.in.read() in the first place?
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
Joined: Mar 20, 2007
Posts: 317
posted
0
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.
This message was edited 1 time. Last update was at by Sridhar Santhanakrishnan
Vishnu Sharma
Ranch Hand
Joined: Feb 03, 2010
Posts: 55
posted
0
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??
This message was edited 1 time. Last update was at by Vishnu Sharma
Vishnu Sharma
Ranch Hand
Joined: Feb 03, 2010
Posts: 55
posted
0
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.
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.
Vishnu Sharma
Ranch Hand
Joined: Feb 03, 2010
Posts: 55
posted
0
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
Joined: Oct 13, 2005
Posts: 25060
posted
0
Rather than System.in.read() try the readLine() methods of BufferedReaders or use a Scanner, which you can read about in the Java™ tutorials.
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
posted
0
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.
This message was edited 1 time. Last update was at by Sridhar Santhanakrishnan
Vishnu Sharma
Ranch Hand
Joined: Feb 03, 2010
Posts: 55
posted
0
Campbell Ritchie wrote:Rather than System.in.read() try the readLine() methods of BufferedReaders or use a Scanner, which you can read about in the Java™ tutorials.