noob question: easiest way to parse an int?

S

Stefan Ram

user said:
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.
 
U

user

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?

Stefan said:
user said:
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.
 
J

jmcgill

user said:
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.
 
S

spbgamer

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
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?

Stefan said:
user said:
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.
 
S

spbgamer

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
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?

Stefan said:
user said:
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.
 
U

user

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??
 
U

user

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.

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
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?

Stefan said:
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.
 
S

Stefan Ram

user said:
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 ); }}
 
S

Stefan Ram

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 ); }}
 
S

Stefan Ram

{ 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«.)
 
S

spbgamer

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
 
S

Stefan Ram

{ 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"«.)
 
M

Mark Space

user said:
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.
 
L

Lasse Reichstein Nielsen

user said:
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
 
O

Oliver Wong

user said:
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
 
L

Luc The Perverse

user said:
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 :)
 
C

Chiappone

user said:
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());
 
S

Stefan Ram

Lasse Reichstein Nielsen said:
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 )); }}
 
T

Tim Smith

user said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top