简体   繁体   中英

Java.Lang.Stringindexoutofboundsexception index out of range (0)

each time the program tries to loop, the error "java.lang.stringindexoutofboundsexception" comes up and highlights

ki=choice.charAt(0);

Does anyone know why that happens?. I'm brand new to programming and this has me stumped. Thanks for any help. Any solution to this problem would be amazing.

import java.util.Date;
import java.util.Scanner;

public class Assignment2
{
    public static void main(String Args[])
    {
        Scanner k = new Scanner(System.in);
        Date date = new Date();
        double Wine = 13.99;
        double Beer6 = 11.99;
        double Beer12 = 19.99;
        double Beer24 = 34.99;
        double Spirit750 = 25.99;
        double Spirit1000 = 32.99;
        int WinePurchase = 0;
        double WineTotal=0.0;
    double GrandTotal = 0.0;
    double GST = 0.0;
    String complete = " ";
    String choice;
    char ki = ' ';




    double Deposit750 = 0.10;
    double Deposit1000 = 0.25;

    System.out.println("------------------------------\n" + 
    "*** Welcome to Yoshi's Liquor Mart ***\nToday's date is " + date);
    System.out.println("------------------------------------\n");
    do{

     if(ki!='W' && ki!='B' && ki!='S')
     {
    System.out.print("Wine is $13.99\nBeer 6 Pack is $11.99\n" +
    "Beer 12 pack is $19.99\nBeer 24 pack is $34.99\nSpirits 750ml is $25.99\n"+
    "Spirits 100ml is $32.99\nWhat is the item being purchased?\n"+
    "W for Wine, B for beer and S for Spirits, or X to quit: ");
   }
    choice = k.nextLine();
    ki= choice.charAt(0);

         switch (ki)
         {
        case 'W':
        {
             System.out.print("How many bottles of wine is being purchased: ");
            WinePurchase = k.nextInt();
            System.out.println();

            WineTotal = Wine*WinePurchase;
            GST = WineTotal*0.05;
            WineTotal += GST;

            System.out.println("The cost of "+WinePurchase+ " bottles of wine including" +
            " GST and deposit is " + WineTotal);

            System.out.print("Is this customers order complete? (Y/N) ");
            complete = k.next();




            break;



        }





        }



}while (ki!='X');

The error means there the index "0" is outside the range of the String. This means the user typed in no input, such as the case when you start the program and hit the enter key. To fix this, simply add the following lines of code:

choice = k.nextLine();
if(choice.size() > 0){
    //process the result
}
else{
    //ignore the result
}

Let me know if this helps!

As you pointed out, the problem is in:

choice = k.nextLine();  
ki= choice.charAt(0);

From the docs nextLine() : "Advances this scanner past the current line and returns the input that was skipped."

So in case the user pressed "enter" the scanner will go to the next line and will return an empty String.

In order to avoid it, simply check if choice is not an empty string:

if (!"".equals(choice)) {
    // handle ki
    ki= choice.charAt(0);
}

Try this:
Your problem was with the Scanner (k) you need to reset it everytime the loop start over.

import java.util.Date;
import java.util.Scanner;

public class Assignment2
{
    public static void main(String Args[])
    {
        Scanner k;
        Date date = new Date();
        double Wine = 13.99;
        double Beer6 = 11.99;
        double Beer12 = 19.99;
        double Beer24 = 34.99;
        double Spirit750 = 25.99;
        double Spirit1000 = 32.99;
        int WinePurchase = 0;
        double WineTotal=0.0;
        double GrandTotal = 0.0;
        double GST = 0.0;
        String complete = " ";
        String choice;
        char ki = ' ';
        double Deposit750 = 0.10;
        double Deposit1000 = 0.25;

        System.out.println("------------------------------\n" + 
        "*** Welcome to Yoshi's Liquor Mart ***\nToday's date is " + date);
        System.out.println("------------------------------------\n");
        do{
            if(ki!='w' && ki!='b' && ki!='s')
            {
                System.out.print("Wine is $13.99\nBeer 6 Pack is $11.99\n" +
                "Beer 12 pack is $19.99\nBeer 24 pack is $34.99\nSpirits 750ml is $25.99\n"+
                "Spirits 100ml is $32.99\nWhat is the item being purchased?\n"+
                "W for Wine, B for beer and S for Spirits, or X to quit: ");
            }
            k= new Scanner(System.in);
            choice = k.nextLine();
            ki= choice.toLowerCase().charAt(0);
            switch (ki)
            {
                case 'w':
                    System.out.print("How many bottles of wine is being purchased: ");
                    WinePurchase = k.nextInt();
                    System.out.println();

                    WineTotal = Wine*WinePurchase;
                    GST = WineTotal*0.05;
                    WineTotal += GST;

                    System.out.println("The cost of "+WinePurchase+ " bottles of wine including" +
                    " GST and deposit is " + WineTotal);

                    System.out.print("Is this customers order complete? (Y/N) ");
                    complete = k.next();
                    break;
            }
            if(complete.toLowerCase().equals("y"))
                break;
        }while (ki!='x');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM