GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Convert string to integer without any  API ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert string to integer without any  API ?" Watch "Convert string to integer without any  API ?" New topic
Author

Convert string to integer without any API ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
if I have a string as "12345", then how to convert it to integer ? we are NOT allowed to use any Integer convert API ?

Thanks.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Edward Chen wrote:if I have a string as "12345", then how to convert it to integer ? we are NOT allowed to use any Integer convert API ?


Check out this formula,

int n = 1*10000 + 2*1000 + 3*100 + 4*10 + 5;

and figure out how you go from "12345" to there.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
uj nossnahoj wrote:
Check out this formula,

int n = 1*10000 + 2*1000 + 3*100 + 4*10 + 5;

and figure out how you go from "12345" to there.

No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Edward Chen wrote:No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.


It's part of a correct solution. What's left are minor details like the conversion of a digit from an ASCII representation to an integer representation for example.

It can be done like this,

char c; // a digit between '0' and '9';
int i = ((int)c) - ((int)'0'); // a digit between 0 and 9.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

uj nossnahoj wrote:char c; // a digit between '0' and '9';
int i = ((int)c) - ((int)'0'); // a digit between 0 and 9.

No need for the cast; char can be implicitly converted to int. Besides, the result of char + char is int anyway.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Agree with Rob. TryIf you miss out the + 1 you get 25 and 0.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Rob Prime wrote:No need for the cast; char can be implicitly converted to int. Besides, the result of char + char is int anyway.


Just because you can omit something doesn't mean you should. I'm very much in favour of explicit casting because it clearly demonstrates your intentions and it removes many possible conversion mistakes like unintentional integer division to name just one.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Edward Chen wrote:No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.


Here's another hint. Note that the formula I posted can be rewritten like,

int n = (((1*10 + 2)*10 + 3)*10 + 4)*10 + 5;

Can you see the pattern? If you consider the digits from left to right you're building up the number in steps. In each step you're multiplying the number from the previous step by ten and adding a digit, like


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I would agree with Rob; the cast is unnecessary and makes the code confusing.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I would also agree that there is no need to cast since the characters already have integer values in the ascii table.

I'm going to try and clarify what Uj is doing here a little to see if it helps.

First step:
Look at the ascii table and determine the values for the characters 1,2,3,4,5

Second step:
Still looking at the ascii table find a value that if you subtract the char values of 1,2,3,4,5 by it you will get the integers 1,2,3,4,5;
Ex: char value of 1 - 'some value' = 1

Step Three:
After you have found those values look closely at the way Uj expanded the number 12345 in his earlier post and see if you can build an integer from that.
i.e.

uj nossnahoj wrote:

Check out this formula,

int n = 1*10000 + 2*1000 + 3*100 + 4*10 + 5;

and figure out how you go from "12345" to there.


Hope this helps,
Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Thank you everybody. I am sure that must be sufficient information to calculate an int value.
 
GeeCON Prague 2014
 
subject: Convert string to integer without any API ?