This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Conversion from ASCII to integer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Conversion from ASCII to integer." Watch "Conversion from ASCII to integer." New topic
Author

Conversion from ASCII to integer.

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
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??


Regards,
Vishnu
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

What do you mean by "ASCII value instead of integer"? The ASCII code of a character is just a number.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
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 : ");
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

Joined: Mar 20, 2007
Posts: 317
In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Why are you using System.in.read() in the first place?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

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
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

Joined: Feb 03, 2010
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

Joined: Feb 03, 2010
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

Joined: Oct 27, 2005
Posts: 19679
    
  18

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
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: 38472
    
  23
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
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

Joined: Feb 03, 2010
Posts: 55
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

Joined: Oct 13, 2005
Posts: 38472
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Conversion from ASCII to integer.