• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion from ASCII to integer.

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "ASCII value instead of integer"? The ASCII code of a character is just a number.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using System.in.read() in the first place?
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Thanks you all.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic