Hi there, I am trying to write a program for a class and I ran into this bug:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -8
at java.lang.String.substring(String.java:1937)
at java.lang.String.substring(String.java:1904)
at CreditCard.main(CreditCard.java:15)
Java Result: 1
My code here:
import java.util.*;
public class CreditCard {
public static void main(String[] args) {
Scanner console = new Scanner (System.in);
System.out.print("Please enter an 8-digit credit card number: ");
String cardNum = console.nextLine(); //43589795 or 73652180
int sum = Integer.parseInt(cardNum.substring(1 + 3 + 5 + 7));
System.out.print(sum); //a simple check to see if code is working
}
}
Any help would be greatly appreciated!
Thanks,
Daren