Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

char to integer conversion

satellite03 (IS/IT--Management) (OP)
25 Jan 04 6:25
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 );
fis.close();

}


}




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


ryder14 (TechnicalUser)
25 Jan 04 15:43
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.
satellite03 (IS/IT--Management) (OP)
25 Jan 04 16:18
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....




ryder14 (TechnicalUser)
25 Jan 04 17:07
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;
byam (Programmer)
25 Jan 04 23:05
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';

satellite03 (IS/IT--Management) (OP)
25 Jan 04 23:06

r u getting the result? as you have suggested i wrote like that ....but it is saying "inconvertible types"...error..
byam (Programmer)
26 Jan 04 0:28
can I see your code?
satellite03 (IS/IT--Management) (OP)
26 Jan 04 6:44
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..

anyway,

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

thanks

 
 

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!

Back To Forum

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