• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conversion from ASCII to integer.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Rob's ideas, Change your loop to check that k is between 48 and 57.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using System.in.read() in the first place?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic