Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Exception help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Exception help

    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


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    974
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Java Exception help

    So, which is line 15 of CreditCard.java?

    According to the stack trace that's the line which caused the ArrayIndexOutOfBoundsException. Such exception occurs whenever you do an operation on a string using character positions which are either negative or past the end of the string.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Exception help

    Line 15 is: int sum = Integer.parseInt(cardNum.substring(1 + 3 + 5 + 7));

    the odd thing is none of these are negetive or past the boundaries of the string. That is, unless I screwed up somewhere. The string is exactly 8 characters

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Exception help

    I figured it out. The compiler was adding all of the values together in order to find one value, which was in turn 8 over the amount of integers available to choose from. Is there a way to get all 4 digits in one line of code? or do I need 4 separate lines?
    Last edited by Daren_Z; October 9th, 2012 at 01:48 AM. Reason: repetition

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    974
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Java Exception help

    No, you'll need to extract the digits of interest one at a time and add their corresponding int values together.

    As always the API docs for substring() will help. Notice how it comes in two flavours.

  6. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Daren_Z (October 9th, 2012)

Similar Threads

  1. java-Exception
    By Brane in forum Exceptions
    Replies: 11
    Last Post: August 10th, 2012, 10:18 PM
  2. Java Null Pointer Exception
    By destructobob in forum Exceptions
    Replies: 5
    Last Post: December 9th, 2011, 12:53 PM
  3. Replies: 5
    Last Post: September 5th, 2011, 11:31 AM
  4. java.net.URL Access Exception
    By MistaWizurd in forum Java Networking
    Replies: 5
    Last Post: April 3rd, 2011, 03:18 AM
  5. Replies: 6
    Last Post: March 25th, 2011, 04:42 PM

Tags for this Thread