1

I'm trying to take a digit from a string and perform operations on it. This is the relevant code I'm working on (updated):

    String cardNumber;
    String curNum;
    long currentNum;
    int firstNum;
    int randNum;
    int i;
    long sum = 0; 

 while (i <= 15) {
            //Delete this line if it doesnt work
            currentNum = Long.parseLong(cardNumber.substring(i));
        if (i % 2 == 0) {
            //currentNum = Integer.parseInt(cardNumber.charAt(i));
            currentNum = currentNum * 2;
            if (currentNum > 9) {
                currentNum = currentNum - 9;
                sum = sum + currentNum;
            }
            else {
                //currentNum = Integer.parseInt(cardNumber.charAt(i));  
                sum = sum + currentNum;
            }   
            i++;
        }
        long lastVal = 10 - (sum % 10);
        char charNum = (char) lastVal;
        //add int to char statement here
        cardNumber = cardNumber + charNum;
    }
        System.out.println(cardNumber);

        userNum--;
    }

The // code is another option I was working on and didn't want to lose, so if there's a way to make it work using that, that's great! The error message this code returns is:

Exception in thread "main" java.lang.NumberFormatException: For input string: "64406849400888" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Long.parseLong(Long.java:692) at java.base/java.lang.Long.parseLong(Long.java:817) at MasterCardGenerator.main(MasterCardGenerator.java:54)

2
  • 4
    267574698286122 is maybe too big to be an int ? Commented Jul 9, 2020 at 5:55
  • @hev1 but he uses it: currentNum = parseInt(....); and later sum = sum + currentNum; (but I admit it is confusing because of the line break before parseInt) Commented Jul 9, 2020 at 5:59

3 Answers 3

2

You said you're trying to take "a" digit from a string. If that's truly the case, and you are trying to examine cardNumber one digit at a time, then you want cardNumber.substring(i, i+1) instead of cardNumber.substring(i).

Read the javadocs for substring(int) and substring(int, int) to understand why.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that the domain of integers is between [-2^31,2^31[, indeed between [-2,147,483,648 to +2,147,483,647]. A possible solution is to parse the string using Long.parseLong(arg)

public class Test{

     public static void main(String []args){
         System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE);
         System.out.println("Long.MAX_VALUE: " + Long.MAX_VALUE);
         Long var = Long.parseLong("267574698286122");
         System.out.println("Printing content of 'var': " + var);
     }
}

And the output is:

Integer.MAX_VALUE: 2147483647
Long.MAX_VALUE: 9223372036854775807
Printing content of 'var': 267574698286122

Comments

1

The problem is that the range string you are trying to format is larger than range of int.

To solve this problem, you need to change the line Integer.parseInt(cardNumber.substring(i)); with Long.parseLong(cardNumber.substring(i));.

See the range off different data types here. Also you can see, for int see here the Integer.MIN_VALUE and Integer.MAX_VALUE and for long see here the Long.MIN_VALUE and Long.MAX_VALUE

UPDATED

As per updated question, String passed is 64406849400888 so valid index for i are 0 <= i <= 13. Apart from that above code looks fine. 64406849400888 can be parsed in long. See it the parsing here

6 Comments

Hi! I just tried that and I'm getting a similar error message! Just to make sure, the code was supposed to look like this, correct? while (i <= 15) { currentNum = Long.parseLong(cardNumber.substring(i)); if (i % 2 == 0) { //currentNum = Integer.parseInt(cardNumber.charAt(i)); currentNum = currentNum * 2; if (currentNum > 9) { currentNum = currentNum - 9; sum = sum + currentNum; } else { //currentNum = Integer.parseInt(cardNumber.charAt(i)); sum = sum + currentNum; } i++; }.
Sorry about the formatting on the previous comment, it wouldn't let me fix anything.
@JennaCrist What is the type of currentNum and sum. It should also be long.
They're both long type!
@JennaCrist Can you add here updated code and exception log?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.