Referring to specific numbers in an integer?

• February 7th, 2013, 04:05 PM
Scorks
Referring to specific numbers in an integer?
Hey! So, I'm working on a credit card validation program, and I was wondering how I can add up specific numbers in an integer. Like for example, if the user inputs 83139638, then I need to add up every second number starting from the right. So it'd be 8 + 6 + 3 + 3 first. How would I go about doing this?

Thanks guys!
Scorks
• February 7th, 2013, 04:11 PM
Norm
Re: Referring to specific numbers in an integer?
How is the number read into the program? If as a String, you could use the String class methods to get at any of the characters/digits in the String.
• February 7th, 2013, 04:16 PM
Scorks
Re: Referring to specific numbers in an integer?
Quote:

How is the number read into the program? If as a String, you could use the String class methods to get at any of the characters/digits in the String.

Well, I have it set as an integer. If I change it to a string, how would I go about doing that?
• February 7th, 2013, 04:20 PM
Norm
Re: Referring to specific numbers in an integer?
I think the String class and the Integer class have methods for converting an int to a String.
• February 7th, 2013, 04:31 PM
Scorks
Re: Referring to specific numbers in an integer?
Code java:

import java.util.Scanner;
public class creditcard
{
public static void main ( String args[] )
{

System.out.println("Please enter your 8-digit credit card number");

Scanner keyboard = new Scanner(System.in);

int number = keyboard.nextInt();
Integer.toString(number);

int sum1=0,sum2=0;

//find individual digits

int chxiii = number.charAt(7);
int chxii = number.charAt(6);
int chxi = number.charAt(5);
int chx = number.charAt(4);
int chix = number.charAt(3);
int chiii = number.charAt(2);
int chii = number.charAt(1);
int chi= number.charAt(0);

//find first sum

sum1 = (chxiii + chxi + chix + chii);

System.out.println(sum1);
}
}

That's my code so far, but it isn't working. Any ideas?

Thanks,
Scorks
• February 7th, 2013, 04:35 PM
Norm
Re: Referring to specific numbers in an integer?
Quote:

it isn't working
Please explain what "isn't working" means?

Do you understand that the char '2' does NOT have an int value of 2? See the ASCII character table.
When working with ASCII characters, you can get the int value by subtracting '0': '3' - '0' = 3
• February 7th, 2013, 04:42 PM
Scorks
Re: Referring to specific numbers in an integer?
Quote:

Please explain what "isn't working" means?

Do you understand that the char '2' does NOT have an int value of 2? See the ASCII character table.
When working with ASCII characters, you can get the int value by subtracting '0': '3' - '0' = 3

Ah, okay. I fixed it a little, and it compiles. When I type in "12345678" as an example, I get 212 as my answer, which isn't correct.

My new code looks like this:
Code java:

import java.util.Scanner;
public class creditcard
{
public static void main ( String args[] )
{

System.out.println("Please enter your 8-digit credit card number");

Scanner keyboard = new Scanner(System.in);

int number = keyboard.nextInt();

String str = Integer.toString(number);

int sum1=0,sum2=0;

//find individual digits

int chxiii = str.charAt(7);
int chxii = str.charAt(6);
int chxi = str.charAt(5);
int chx = str.charAt(4);
int chix = str.charAt(3);
int chiii = str.charAt(2);
int chii = str.charAt(1);
int chi= str.charAt(0);

//find first sum

sum1 = ((chxiii) + (chxi) + (chix) + (chii));

System.out.println(sum1);
}
}

So as you said, the string value doesn't equal the int value. How would I fix this, so that I get 20 instead of 212?
• February 7th, 2013, 04:50 PM
Norm
Re: Referring to specific numbers in an integer?
Did you see the last line of my post?
• February 7th, 2013, 05:00 PM
Scorks
Re: Referring to specific numbers in an integer?
Quote:

Did you see the last line of my post?

Ah, nope - sorry.