0

So I've tried looking through other threads with similar problems, but I couldn't really relate them to my current problem as most of them were suggesting solutions that I haven't been currently taught in my Computer Science 1 college class.

Anyways, if you'll look at my code, I'm trying to get user input to either continue the code and do more calculations, or to stop the program. They'll either type y for yes to continue or any other character to terminate the program. Everything seems to work up until after I enter my next value for cardNumber. It uses my service class, does the math and everything, and then asks if I want to continue again but then I'm told.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:44)
at java.base/java.lang.String.charAt(String.java:692)
at CreditCardDemo.main(CreditCardDemo.java:48)

Now I looked through other threads as I said, and have seen similar errors before, but I still don't understand what this error is telling me or how to fix it. I'm looking at line 48 like the error suggests and I still don't understand why I'm getting this error, especially since I used the

    repeat = input.charAt(0);
    kb.nextLine(); 

previously in my code and it worked fine...

I'm really sorry if there's some thread I missed that's super explanatory and would have helped me but I wasn't able to find it if it existed. Can anyone help me out here?

public class CreditCardDemo
{
   public static void main(String [] args)
   {
      int cardNumber;
      String input;
      char repeat;

      Scanner kb = new Scanner(System.in);

      CreditCard card1 = new CreditCard(67034014);
      CreditCard card2 = new CreditCard();

      System.out.print("Enter 8 digit credit card number: " + card1.getCardNumber());
      card1.outputTest();
      System.out.println();

      System.out.println("Continue(y/n)?");
      input = kb.nextLine();
      repeat = input.charAt(0);
      if (repeat != 'y')
      {
         System.exit(0);
      }


      do
      {
            System.out.print("Enter 8 digit credit card number: ");
            card2.setCardNumber(kb.nextInt()); 
            card2.outputTest();

            System.out.println("Continue(y/n)?");
            input = kb.nextLine();
            repeat = input.charAt(0);
            kb.nextLine();
      }
         while (repeat == 'y');

   }
}
Jacky
  • 27
  • 6
  • Which line is 48? – markspace Sep 26 '18 at 18:23
  • repeat = input.charAt(0); It's in the do part of the code. – Jacky Sep 26 '18 at 18:23
  • maybe length of input in 0. So you are getting error. – Vikash Kumar Verma Sep 26 '18 at 18:24
  • 1
    I think I see what is going on. The string `input` is zero size at that point. `charAt(0)` reads the first character, but there is no first character, because the string has no characters at all. Hence, 0 is out of bounds. – markspace Sep 26 '18 at 18:25
  • you should place the nextline in the while() part... i think – Randy Sep 26 '18 at 18:25
  • What should I change about the code then? The entire statement or just the value of chartAt()? – Jacky Sep 26 '18 at 18:25
  • What you really should do is validate your input instead of blindly accepting anything in the input buffer. But that is possibly more sophisticated than you are interested in. – markspace Sep 26 '18 at 18:26
  • I'm just trying to follow my assignment as closely as I can, what can I do with my current code to get the solution I need? – Jacky Sep 26 '18 at 18:27

0 Answers0