Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Referring to specific numbers in an integer?

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referring to specific numbers in an integer?

    Quote Originally Posted by Norm View Post
    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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referring to specific numbers in an integer?

    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Referring to specific numbers in an integer?

    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
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referring to specific numbers in an integer?

    Quote Originally Posted by Norm View Post
    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:
    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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Referring to specific numbers in an integer?

    Did you see the last line of my post?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Referring to specific numbers in an integer?

    Quote Originally Posted by Norm View Post
    Did you see the last line of my post?
    Ah, nope - sorry.

Similar Threads

  1. Counting the amount of zeroes, odd and even numbers in an integer
    By 54stickers in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 07:17 PM
  2. Referring to cases
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2011, 07:41 AM
  3. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 03:30 PM
  4. OS Specific Jars
    By sinistaDev80 in forum Java Theory & Questions
    Replies: 0
    Last Post: March 26th, 2011, 10:29 AM
  5. Selecting a specific list
    By sapzero in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2011, 12:06 PM

Tags for this Thread