0

In case I have to calculate with 16 digits of number maybe 4388576018402626

I want to calculate in three way

way 1. (part14 * 2 + part12 * 2 + part10 * 2 + part8 * 2 + part6 * 2 + part4 * 2 + part2 * 2 +part0 * 2 ) =37
however it outcome error

way 2. (part15 + part13 + part11 + part9 + part7 + part5 + part3 +part1) = 38 however it outcome 66080783

way 3. (way1 + way2) %10 because way 1 error it cannot have any outcome

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //code for loop of follow program
    boolean run = true;
    while(run){

    //User enter the data
    Scanner sc = new Scanner (System.in);
    System.out.print("Enter the credit card number:");

    String cs = sc.nextLine();

    //String divide the 16 number in to one  different part
    String[] parts = cs.split("");
    String part0 = parts[0]; 
    String part1 = parts[1]; 
    String part2 = parts[2]; 
    String part3 = parts[3]; 
    String part4 = parts[4]; 
    String part5 = parts[5];
    String part6 = parts[6]; 
    String part7 = parts[7]; 
    String part8 = parts[8];
    String part9 = parts[9]; 
    String part10 = parts[10]; 
    String part11 = parts[11]; 
    String part12 = parts[12]; 
    String part13 = parts[13]; 
    String part14 = parts[14];
    String part15 = parts[15]; 

    String sd = (part14 * 2 + part12 * 2 + part10 * 2 + part8 * 2 + part6 * 2 + part4 * 2 + part2 * 2 +part0 * 2 );
    String sd1 = (part15 + part13 + part11 + part9 + part7 + part5 + part3 +part1);
    String sd2 = (sd+sd1);

    int sd3 = (sd2%10);

    if (sd3 =0)
    System.out.println ("The card is valid");
    else
    System.out.println ("The card is invalid");

}
}

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
T Lam
  • 13
  • 3
  • Please add the error message you are receiving. I assume it is some kind of type error. Hint: You have to parse your strings as integers. And please make sure you understand the tags you added to this question. – ventiseis Apr 13 '16 at 20:21
  • 1
    You can't do arithmetic on strings. Convert the strings to numbers, then do your calculation – smac89 Apr 13 '16 at 20:34
  • so what can i do for calculate will part of 16digit – T Lam Apr 14 '16 at 11:04

1 Answers1

0

Whaat? :) Why don't you use BigDecimals if you wish to do arithmetic with numbers which can not fit to int or double?

Or what is your goal exactly? No one calculates with Strings. Strings are nice to store arbitrary things including card numbers :) but to do math, you always need something numeric.

String sd = (part14 * 2 ) is not something which works.
Integer.valueOf(part14)*2 does work.

Also consider using the charAt method of a string to get any character, instead of make part1...part999999 strings.

Considering the code snippet, I guess your question is "how to calculate the luhn number" or "how to validate a credit card number in java". A good question opens the way to a good answer, see e.g. Check Credit Card Validity using Luhn Algorithm for an example.

Community
  • 1
  • 1
Gee Bee
  • 1,794
  • 15
  • 17
  • because i want to make the programme with input 16 digit and divide to 16 part for calculate. However i only know use in String and part to write – T Lam Apr 14 '16 at 11:06
  • You don't need to split it into 16 parts - you can use the charAt() method of any string to get a character at any position. This, together with a simple for-loop allows your code to work with arbitrary length of input strings (and much-much shorter to write, too). I suggest you to consult with Java trails on Oracle - that is quite a decent description of the language. Java and its features is out there to help you and make your life *much* easier. I found it worth of learning it well. Have fun with Java! – Gee Bee Apr 15 '16 at 23:50