Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char to integer conversion

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
0
0
IN
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

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


 
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.
 
ok....you are telling to cast into string!!...but string is an object reference ....did you mean

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


or did you mean
Code:
....
.....
String oneByte;
..............
oneByte = (String) fis.read();

int n    = Integer.parseInt(oneByte);

this is also giving error....




 
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;
 
I don't think you can cast a primitive type to Object.

I would do the following:

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

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

r u getting the result? as you have suggested i wrote like that ....but it is saying "inconvertible types"...error..
 
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



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top