Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


char to integer conversion

char to integer conversion

char to integer conversion

hi ,

i think a sigle character is the smallest string.

but when i wrote to convert this charater to integer it gave me error.

i wrote

import java.io.*;

class ReadFileExample

public static void main( String args[]) throws IOException
char oneByte;    
File f = new File ("d:\\data.txt");    
FileInputStream fis = new FileInputStream (f) ;
oneByte = (char) fis.read();

int n    = Integer.parseInt(oneByte);

System.out.println("integer =" + n );



although parseInt() demands a string , but a single character is also can be treated as a string. right?

then why it could not convert the character into integer??

my file
400 25.81
600 33.82
800 40.94
1000 51.62

compilation erroe
javac ReadFileExample.java
ReadFileExample.java:13: cannot resolve symbol
symbol  : method parseInt (char)
location: class java.lang.Integer
int n    = Integer.parseInt(oneByte);
1 error
>Exit code: 1

whats wrong with this???

is there any  atoi()  function (as in C )  in java which can convert char to int????

i have no problem to extract the integer but i was intentionally trying to  convert the char into integer by using methods. why i failed???

RE: char to integer conversion

The problem you are running into is that Java has the primitive variable type char and an object reference variable type String. The Integer.parseInt() will work with a string of any length but not a char, so cast oneByte to a String instead of a char and this should work.

RE: char to integer conversion

ok....you are telling to cast into string!!...but string is an object reference ....did you mean

int n= Integer.parseInt((String)oneByte);-->error

or did you mean

String oneByte;
oneByte = (String) fis.read();

int n    = Integer.parseInt(oneByte);

this is also giving error....

RE: char to integer conversion

The rough work around would be to use your original cast to a char and add a line that recasts the char to another variable of the type String.

oneByte = (char)fis.read();

strOneByte = (String)oneByte;

RE: char to integer conversion

I don't think you can cast a primitive type to Object.

I would do the following:

oneByte = (char)fis.read();
strOneByte = String.valueOf(oneByte);
int n    = Integer.parseInt(strOneByte);

If you just want to get the integer from a char, a simpler more efficient way would be:

oneByte = (char)fis.read();
int n    = oneByte - '0';

RE: char to integer conversion


r u getting the result? as you have suggested i wrote like that ....but it is saying "inconvertible types"...error..

RE: char to integer conversion

can I see your code?

RE: char to integer conversion

To byam
it was not pointing to you... it was to  ryder14.
i did not see your post while posting...probabily mine and yours post has gone simultaneously..


i run your code
yes, your code is working perfectly.



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close