I’m having string consisting of a sequence of digits (e.g. "1234"). How to return the String as an int without using Java’s library functions like Integer.parseInt?
public class StringToInteger {
public static void main(String [] args){
int i = myStringToInteger("123");
System.out.println("String decoded to number " + i);
}
public int myStringToInteger(String str){
/* ... */
}
}
And what is wrong with this?
EDIT :
If you really need to do the conversion by hand, try this:
The above will work fine for positive integers, if the number is negative you’ll have to do a little checking first, but I’ll leave that as an exercise for the reader.