I'm looking to find a way to convert a string to an int in order to then extract and return the first 4 digits in this int.
Note: It must remain as a String for the other methods to work properly, though.
Try following:
String str = "1234567890";
int fullInt = Integer.parseInt(str);
String first4char = str.substring(0,4);
int intForFirst4Char = Integer.parseInt(first4char);
Wherever you want integer for first four character use intForFirst4Char
and where you wanna use string use appropriate.
Hope this helps.
Integer.parseInt(myIntegerString.substring(0, 4))
Also, read the JDK:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With