Java Forum / General / July 2006
noob question: easiest way to parse an int?
user - 19 Jul 2006 05:24 GMT say i got an int, 530: what could i do to get back 2nd digit [the 3 in this case]?
Stefan Ram - 19 Jul 2006 05:31 GMT >say i got an int, 530: what could i do to get back 2nd digit >[the 3 in this case]? Numbers do not have digits. Numerals might have digits.
A numeral is a numeric literal, i.e., a textual representation of a number in specific notation (numeral system). However, a numeral system has to be chosen, for example, a place-value system, for example, the common decimal place-value system.
The meaning of »2nd digit« depends on where you start to count the digits from: from the left or from the right. In the special case of the numeral »530« this does not matter, but for the numeral »5320« it might make a difference.
user - 19 Jul 2006 05:42 GMT if the number was 5320, and it was counted left to right, id ask for the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt matter what direction its counted from, i just need some way of getting that value. by saying that im confusing the definition of numbers and numerals does not help me get the value i need.
would the easiest way be to change the int into a string, break the string up, then give back the interger representation of the digit im looking for?
> >say i got an int, 530: what could i do to get back 2nd digit > >[the 3 in this case]? [quoted text clipped - 10 lines] > special case of the numeral »530« this does not matter, but > for the numeral »5320« it might make a difference. jmcgill - 19 Jul 2006 05:44 GMT > would the easiest way be to change the int into a string, break the > string up, then give back the interger representation of the digit im > looking for? I'm pretty sure I'd do something that involves successive integer divides by 10 although converting to string is not a horrible choice.
user - 19 Jul 2006 05:49 GMT that sounds like it could work. if i divided by 10 twice, and got the remainder of 930, i would have the "2nd" digit. is there a method that lets me get the remainder of a number diving into another??
> > would the easiest way be to change the int into a string, break the > > string up, then give back the interger representation of the digit im > > looking for? > > I'm pretty sure I'd do something that involves successive integer > divides by 10 although converting to string is not a horrible choice. Oliver Wong - 19 Jul 2006 15:57 GMT > that sounds like it could work. if i divided by 10 twice, and got the > remainder of 930, i would have the "2nd" digit. is there a method that > lets me get the remainder of a number diving into another?? Not a method, but there is an operator: %
27 % 5 = 2
- Oliver
spbgamer@yahoo.com - 19 Jul 2006 05:47 GMT How about this. I wasn't sure if you wanted a String or an int...so I did both.
int number = 530;
String sNumber = String.valueOf(number); String sFirstDigit = sNumber.substring(0,1); String sSecondDigit = sNumber.substring(1,2); String sThirdDigit = sNumber.substring(2,3);
int firstDigit = Integer.parseInt(sFirstDigit); int secondDigit = Integer.parseInt(sSecondDigit); int thirdDigit = Integer.parseInt(sThirdDigit);
System.out.println(sFirstDigit + " " + sSecondDigit + " " + sThirdDigit); System.out.println(firstDigit + " " + secondDigit + " " + thirdDigit);
Output: 5 3 0 5 3 0
> if the number was 5320, and it was counted left to right, id ask for > the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt [quoted text clipped - 20 lines] > > special case of the numeral »530« this does not matter, but > > for the numeral »5320« it might make a difference. spbgamer@yahoo.com - 19 Jul 2006 05:47 GMT How about this. I wasn't sure if you wanted a String or an int...so I did both.
int number = 530;
String sNumber = String.valueOf(number); String sFirstDigit = sNumber.substring(0,1); String sSecondDigit = sNumber.substring(1,2); String sThirdDigit = sNumber.substring(2,3);
int firstDigit = Integer.parseInt(sFirstDigit); int secondDigit = Integer.parseInt(sSecondDigit); int thirdDigit = Integer.parseInt(sThirdDigit);
System.out.println(sFirstDigit + " " + sSecondDigit + " " + sThirdDigit); System.out.println(firstDigit + " " + secondDigit + " " + thirdDigit);
Output: 5 3 0 5 3 0
> if the number was 5320, and it was counted left to right, id ask for > the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt [quoted text clipped - 20 lines] > > special case of the numeral »530« this does not matter, but > > for the numeral »5320« it might make a difference. user - 19 Jul 2006 06:01 GMT thanks, this looks pretty simple compared to the version i had previously.
> How about this. I wasn't sure if you wanted a String or an int...so I > did both. [quoted text clipped - 42 lines] > > > special case of the numeral »530« this does not matter, but > > > for the numeral »5320« it might make a difference. Stefan Ram - 19 Jul 2006 06:12 GMT >if the number was 5320, and it was counted left to right, id >ask for the 2nd digit, In general, not every number has a 2nd digit in this sense. For example, »7« does not. With that in mind, the following program shows to ways to print the »3«.
public class Main { public void run( final java.lang.String[] args ) { java.lang.System.out.println( 5320 / 100 % 10 ); java.lang.System.out.println( Integer.toString( 5320 ).charAt( 1 )); }
public static void main( final java.lang.String[] args ) { new Main().run( args ); }}
Stefan Ram - 19 Jul 2006 06:19 GMT >public class Main >{ public void run( final java.lang.String[] args ) > { java.lang.System.out.println( 5320 / 100 % 10 ); > java.lang.System.out.println( Integer.toString( 5320 ).charAt( 1 )); } > public static void main( final java.lang.String[] args ) > { new Main().run( args ); }} Somewhat more general and possibly fast than method calls:
public class Main { public void run( final java.lang.String[] args ) { int pot[] = new int[]{ 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; java.lang.System.out.println( 5320 / pot[ 2 ]% 10 ); }
public static void main( final java.lang.String[] args ) { new Main().run( args ); }}
Stefan Ram - 19 Jul 2006 06:23 GMT > { int pot[] = new int[]{ 1, 10, 100, 1000, 10000, 100000, > 1000000, 10000000, 100000000, 1000000000 }; > java.lang.System.out.println( 5320 / pot[ 2 ]% 10 ); } The above method might no work for negative numbers.
(Spelling corrections: In the post preceding my preceding post, change the last »to« to »two«, and in the preceding post change »fast« to »faster«, and in this post change the first »no« to »not«.)
spbgamer@yahoo.com - 19 Jul 2006 06:33 GMT The divide by 10 idea interested me. This seems to work for both positive and negative numbers.
int aNumber = 24530; int number = Math.abs(aNumber);
Vector digits = new Vector(); digits.add(new Integer(number % 10)); int newNumber = number / 10; while (newNumber > 0) { digits.add(0, new Integer(newNumber % 10)); newNumber /= 10; }
for (int i = 0; i < digits.size(); i++) { Integer digit = (Integer)digits.elementAt(i); System.out.println("Digit["+i+"] = " + digit.intValue()); }
Output:
Digit[0] = 2 Digit[1] = 4 Digit[2] = 5 Digit[3] = 3 Digit[4] = 0
Stefan Ram - 19 Jul 2006 06:34 GMT >> { int pot[] = new int[]{ 1, 10, 100, 1000, 10000, 100000, >> 1000000, 10000000, 100000000, 1000000000 }; >> java.lang.System.out.println( 5320 / pot[ 2 ]% 10 ); } The array should be static, so that it is built only once:
public class Main { final static int pot[] = new int[] { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; public void run( final java.lang.String[] args ) { java.lang.System.out.println( 5320 / pot[ 2 ]% 10 ); }
public static void main( final java.lang.String[] args ) { new Main().run( args ); }}
(I have to correct my spelling correction regarding the »last "to"«: This should have been »the "to" before the last "to"«.)
Lasse Reichstein Nielsen - 19 Jul 2006 10:58 GMT > if the number was 5320, and it was counted left to right, id ask for > the 2nd digit, if it was right to left, id ask for the 3rd. it doesnt > matter what direction its counted from, i just need some way of getting > that value. by saying that im confusing the definition of numbers and > numerals does not help me get the value i need. So you want the second most significant digit in the decimal representation of the number.
Let's assume that you want the same for negative numbers, and if the number's numeric value is less than 10, you want 0. Those should be special-case'ed. After that, it's just a matter of finding the number of digits and shift the number downwards appropriately.
static int secondMostSignificantDecimalDigit(int number) { if (number < 0) { number = - number; } if (number < 0) { return 1; } // MIN_VALUE == -2147483648 if (number < 10) { return 0; } int magnitude = (int) Math.floor(Math.log10(number)); return ((int)(number / Math.pow(10, magnitude - 1))) % 10; }
Good luck. /L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Stefan Ram - 19 Jul 2006 19:52 GMT >int magnitude = (int) Math.floor(Math.log10(number)); >return ((int)(number / Math.pow(10, magnitude - 1))) % 10; My previous solution was not that general, because it would only find the second digit of numbers between 1000 and 9999.
Here is my attempt to extend it to numbers larger than 10, while still not calling methods of the Java SE and not using iteration. I hope that it will be fast this way.
public class Main { final static int pot[] = new int[] { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
public int secondDigitOf( final int number /* must be >= 10 */ ) { int n = number; int l = 0; if( n >= 100000000 ){ l += 8; n /= 100000000; } if( n >= 10000 ){ l += 4; n /= 10000; } if( n >= 100 ){ l += 2; n /= 100; } if( n >= 10 ){ l += 1; n /= 10; } return number / pot[ l - 1 ]% 10; }
public static void main( final java.lang.String[] args ) { java.lang.System.out.println( new Main().secondDigitOf( 5320 )); }}
Lasse Reichstein Nielsen - 20 Jul 2006 13:26 GMT > { final static int pot[] = new int[] > { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; [quoted text clipped - 6 lines] > if( n >= 10 ){ l += 1; n /= 10; } > return number / pot[ l - 1 ]% 10; } Why the indirection through an array lookup, if efficiency is important? You should get the same effect from:
public int secondDigitOf( final int number /* must be >= 10 */ ) { int n = number; int m = 1; if ( n >= 100000000 ) { m *= 100000000; n /= 100000000; } if ( n >= 10000 ) { m *= 10000; n /= 10000; } if ( n >= 100 ) { m *= 100; n /= 100; } if ( n >= 10 ) { m *= 10; n /= 10; } return (number / (m/10)) % 10; } }
/L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Mark Space - 19 Jul 2006 07:08 GMT > say i got an int, 530: what could i do to get back 2nd digit [the 3 in > this case]? I don't know about the easiest or best, but it happens I've was playing with number and characters recently.
int anInt = 530; int aDigit = Integer.toString(anInt).charAt(1) - '0';
The charAt method starts at offset 0 for the right most character, so use position - 1 for the digit to extract. For the second digit, use 2 - 1 = 1.
Negative numbers will probably give this code hairballs (they'll have a - as the first character, I expect). And very large or very small numbers will also be troublesome since they may be in scientific (exponential) notation.
But the above one-liner is short and to the point.
Luc The Perverse - 19 Jul 2006 16:09 GMT > say i got an int, 530: what could i do to get back 2nd digit [the 3 in > this case]? int v = 530; int secondDigit = (v % 100) / 10;
or
String v = "530"; int secondDigit = v.substring(v.length()-2).charAt(0) - '0';
or
String V = "530"; int v = java.lang.Integer.parseInt(V); int secondDigit = (v % 100) / 10;
This problem should be trivial - perhaps you need to better familiarize yourself with the mathmatical operations of division and modulus. They are very useful for things like this :)
 Signature LTP
for( Base i : allYourBase) i.AreBelongToUs();
Chiappone - 19 Jul 2006 16:16 GMT > say i got an int, 530: what could i do to get back 2nd digit [the 3 in > this case]? int number = 530; String sdigit = new Character(String.valueOf(number).charAt(1)).toString(); int idigit = Integer.parseInt(new Character(String.valueOf(number).charAt(1)).toString());
Tim Smith - 20 Jul 2006 04:50 GMT > say i got an int, 530: what could i do to get back 2nd digit [the 3 in > this case]? If N is the variable with the int, and you are counting from the right, then you could try this:
(N/10)%10
Similarly, (N/100)%10 would be the 5, and (N/1)%10 would be the 0.
 Signature --Tim Smith
Martin Gregorie - 20 Jul 2006 11:44 GMT >> say i got an int, 530: what could i do to get back 2nd digit [the 3 in >> this case]? [quoted text clipped - 5 lines] > > Similarly, (N/100)%10 would be the 5, and (N/1)%10 would be the 0. Thats asking to be generalized as:
/** * Return the nth digit from value. Digits are counted from the right. * The rightmost digit is zero. -1 is returned of n doesn't reference * a digit within the number. */ int getDigit(int value, int n) { int i = 1; int digit = -1;
value = Maths.abs(value); if (n >= 0) { while ((value/(10*i) > 0) i++;
digit = (n >= i ? -1 : value/(10*n)%10); } return digit; }
The absolute value is only required for the range test on n to work.
 Signature martin@ | Martin Gregorie gregorie. | Essex, UK org |
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|