Results 1 to 3 of 3
  1. #1
    dtacker is offline Member
    Join Date
    Oct 2015
    Location
    Tennessee
    Posts
    4
    Rep Power
    0

    Default Multiplication with individual elements in an array.

    So I'm working on this project that is going to validate a credit card using the Luhn check. I'm required to write a ValidateMyCard program that implements a class ValidateCard that uses the Luhn algorithm to check the credit card.

    So far I've constructed the ValidateMyCard program. This part asks for a credit card from the user. It then takes this credit card and puts it into an array.
    That array is then reversed to aid in the first step of the Luhn algorithm.

    From there I created a new class that will contain my Luhn algorithm steps. The first step is to double every second digit and then any doubling that results in a two-digit number will be added together to get a single digit number.

    What is the best way to go about multiplying every second element in an array by (2)? I'm unsure of which way to go from here.

    Java Code:
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Collections;
    
    
    public class ValidateMyCard {
        public static void main (String[] args) {
            
            Scanner input = new Scanner(System.in);
            
          
            //Declares array list that is 16 digits
            int [] numberList = new int[16];
            
            
            //Tells user to enter a credit card number
            System.out.print("Enter a credit card number: ");
            String numbers =input.nextLine();
            
            //Converts string into a character array
            char[] charArray = numbers.toCharArray();
            
            
            //reverses the order of the character array into a new array
            char[] reversedNumbers = new StringBuilder(new String(charArray)).reverse().toString().toCharArray();
    
            
            //loop that prints new reversed array
            for (int i=0; i < reversedNumbers.length; i ++) {
                
                System.out.println(reversedNumbers[i]);
                
            
            }    
            
    }
    
    class ValidateCard {
        
     
      
        
    }
    Last edited by dtacker; 10-19-2015 at 06:53 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Multiplication with individual elements in an array.

    You should only double every second digit (according to your description), not every digit ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    dtacker is offline Member
    Join Date
    Oct 2015
    Location
    Tennessee
    Posts
    4
    Rep Power
    0

    Default Re: Multiplication with individual elements in an array.

    correct, my mistake.

Similar Threads

  1. Printing values from Array on individual lines
    By Castle in forum New To Java
    Replies: 2
    Last Post: 11-28-2012, 01:43 AM
  2. Array multiplication table
    By nonybrighto in forum New To Java
    Replies: 7
    Last Post: 10-21-2012, 09:21 PM
  3. array multiplication with single double
    By Tien in forum New To Java
    Replies: 1
    Last Post: 10-25-2011, 12:21 PM
  4. int array multiplication
    By sternhagenr in forum New To Java
    Replies: 10
    Last Post: 08-20-2011, 01:41 AM
  5. I need fast multiplication of array???
    By lulzim in forum Advanced Java
    Replies: 1
    Last Post: 04-05-2011, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •