-1

when i use s.charAt(0) while s is an string input from the user, I get this as an error even though the program runs the first half of the program.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at Shopping.main(Shopping.java:22)

What's the solution to this program? here is my code.

import java.util.Scanner;
public class Shopping {
public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Programmed by Raymond Lee");
    System.out.println("Welcome to Shopper's Paradise");
    ShoppingCart cart = new ShoppingCart();
    System.out.print("Enter the name of the first item: ");
    String item = keyboard.nextLine();
    System.out.print("Enter the quantity: ");
    int quantity = keyboard.nextInt();
    System.out.print("Enter the price: ");
    double price = keyboard.nextDouble();
    cart.addToCart(item, price, quantity);
    System.out.print("Enter the name of the next item or Q to quit: ");
    String quit = keyboard.nextLine();
    char choice = quit.charAt(0);
    while((choice != 'Q' && choice != 'q') || quit.length() != 1) {
        quit = item;
        System.out.print("Enter the quantity: ");
        quantity = keyboard.nextInt();
        System.out.print("Enter the price: ");
        price = keyboard.nextDouble();
        cart.addToCart(item, price, quantity);
        System.out.print("Enter the name of the next item or Q to quit: ");
        quit = keyboard.nextLine();
        choice = quit.charAt(0);
    }           
    System.out.println(cart);
}
}
user207421
  • 289,834
  • 37
  • 266
  • 440
rleekc
  • 7
  • 1
  • Possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Mick Mnemonic Oct 12 '15 at 00:11
  • As the solutions in the duplicate question indicate, the simplest way to fix your code is to use `keyboard.nextLine()` with a corresponding parsing method for numerical user input: `quantity = Integer.parseInt(keyboard.nextLine());` and `price = Double.parseDouble(keyboard.nextLine());`. The problem is occurring because `Scanner.nextInt()` and `Scanner.nextDouble()` do not consume the last newline character in `System.in`. – Mick Mnemonic Oct 12 '15 at 00:20
  • Stupid title. `"string.java:658"` isn't a problem that needs a solution. It is a file/line number reference. – user207421 Oct 12 '15 at 00:52

1 Answers1

0

The error is occuring at this line

char choice = quit.charAt(0);

This is due to the fact that when you call

double price = keyboard.nextDouble();

then nextDouble leaves the newline in the input stream. So when following is called

String quit = keyboard.nextLine();

then result of nextLine is empty string, results in the given error when you try to use charAt method.

To resolve this error, simply change following

String quit = keyboard.nextLine();

To

String quit = keyboard.next();

Hope this helps

Balwinder Singh
  • 2,196
  • 5
  • 20
  • 32