aspose file tools*
The moose likes Beginning Java and the fly likes convert char to int in a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "convert char to int in a string?" Watch "convert char to int in a string?" New topic
Author

convert char to int in a string?

xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
Hi guys,
i got another knotty question. the program below might execute with no problem, but the real problem i facing is how do i convert a char to int?
i intentally force an int into the int by >> num1 = first - '0';
but from my tutor, this is not the right way.



[ August 24, 2007: Message edited by: xiao sean ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Did your tutor explain why he is not satisfied with your solution? Because it works as you want it, and it's not really wrong.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
oh, he say, something wrong with this sentence.

ask me to amend.

Hint is a convert string..

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
I suspect the point your teacher is trying to make is that converting a char to its numeric value by subtracting the ASCII value for 0 from it is relying on the way the ASCII table is implemented ie that the number characters are contiguous and in numeric order starting from 0. Now we all know this is so and they aren't going to change the ASCII table but the point is it's not good practice to rely on the implementation detail.

I suggest you look at the java.Character class and in particular the getNumericValue(..) method.
prakash chandra
Greenhorn

Joined: Aug 14, 2007
Posts: 24
Another way is there to convert a char to int.

char c = '5';
int i = c; // char can be easily assigned to int.
i = i - 48; // ascii of zero is 48.

o/p
--------
i = 5;


SCJP 6.0
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
don't get it actually.


i sort of change the code to meet you.


[ August 24, 2007: Message edited by: xiao sean ]
[ August 24, 2007: Message edited by: xiao sean ]
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2287
    
  49
Originally posted by prakash chandra:
Another way is there to convert a char to int.

char c = '5';
int i = c; // char can be easily assigned to int.
i = i - 48; // ascii of zero is 48.

o/p
--------
i = 5;
This is even worse than the original post, it's adding another level of implementation knowledge to the code ie that 48 is the ASCII value of the chracter '0'. You shouldn't be interested in such detail, Java provides methods to distance you from the particular implementation so that your code will work regardless of which character table is being used.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Here is an idea:

Instead of handling the five characters separately, use Integer.parseInt(...) to convert the whole input string to a number. Then use that number to do the calculations you want to do with it. Is that more in line with what your tutor means?
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
hi young,

by following your hint.

can please tell me how do i carry on?


i only know how to display args but not sure on how to use it in expression.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Why args[4]? That is the fifth command line argument. You want to parse the first command line argument - args[0]. args is an array of String objects. What is "args[0] *= 5" supposed to mean? That doesn't work.

int number = Integer.parseInt(args[0]);

Now, if I look at your code above I see that you want to do some calculations with the digits of the number. Think about how you are going to do that calculation if you have the number.
[ August 24, 2007: Message edited by: Jesper Young ]
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
i'm struck big time.


because from what i know,
int number = Integer.parseInt(args[0]);

only declare a int..

but my for case,
i need to input
Eg.
a number - 10274

from the input it will draw each digit out to do calculation and compare.

so if i use int number = Integer.parseInt(..) command,
it will recongise as a number only. how am i suppose to draw each digit out?

struck.....
[ August 25, 2007: Message edited by: xiao sean ]
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
i found another new way of converting char to int.


from what i read, the 10 is a base 10, in another words, convert the char to a digit if it's true.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert char to int in a string?