Domanda

I'm struggling to figure out what the compile/syntax error is in my code.

public class CreditCardValidation {

    public static void main (String[] args){

        System.out.print(prefixMatched(4388576018402626, 4388));

    }

    /*
    Return the number of digits in d
    */

    public static int getSize(long d) {

        int size = 0 ;

        while( d > 0 ) {
            d = d / 10 ;
            size = size + 1 ;       
        }
        return size ;
    }

    /*
    Return the first k number of digits from number. If the number of digits in number is 
    less than k, return the number.
    */

    public static long getPrefix(long n, int k) {

        int f = getSize(n)-k;

        long prefix = n/((int)(Math.pow(10, f)));

        return prefix;
        }

    /*
    Return true if the digit d is a prefix for number.
    */

    public static boolean prefixMatched( long number, int d ) {

        if ( d == getPrefix(number, 4))

            return true ;
        else
            return false ;

        }

    }

As you can see I'm trying to call prefixMatched to check whether the credit card number meets the requiremen; if digit d is a prefix for number. However, the only thing I get back from the compiler is:

"CreditCardValidation.java:6: integer number too large: 4388576018402626
        System.out.print(prefixMatched(4388576018402626, 4388));


                               ^"

I'm sorry if my question is too vauge, this is my first post.

È stato utile?

Soluzione 2

The problem is that you are specifying an integer literal 4388576018402626 and that number is larger than the maximum integer, 2147483647.

You are attempting to pass it to a method that takes a long, so make it a long literal by appending L:

System.out.print(prefixMatched(4388576018402626L, 4388));

The JLS specifies this behavior in Section 3.10.1:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

and

It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).

Altri suggerimenti

  1. You need to indicate to the compiler that your constant (the CC number) is a long. Put an L on the end of the constant.
  2. It's actually a little easier to treat CC numbers as strings and use charAt(x) to calculate check digits.

add L at the end of your literal :

4388576018402626L

You should use Long instead of Integer

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The unsigned long has a minimum value of 0 and maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

You should use following to avoid exception:

System.out.print(prefixMatched(4388576018402626L, 4388));

The L indicates given value is long. You can use either l or L but I prefer to use L because it looks goods while looking code.

Source: Oracle Docs.

For this code, I would recommend using String instead of dealing with long and int. It's far easier when trying to match the first four digits, which can be isolated easily using String#substring: (String_name).substring(0,4) will be return first four digits of the String. This can then be parsed as an int using Integer#parseInt, or simply compared to another String (if the prefix were a String as well).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top