Java.Lang.Stringindexoutofboundsexception索引超出范围(0)

Java.Lang.Stringindexoutofboundsexception索引超出范围(0),java,exception,Java,Exception,每次程序尝试循环时,都会出现并突出显示错误“java.lang.stringindexoutofboundsexception” ki=选择字符(0) 有人知道为什么会这样吗?。我是编程新手,这让我很困惑。谢谢你的帮助。这个问题的任何解决方案都将是惊人的 import java.util.Date; import java.util.Scanner; public class Assignment2 { public static void main(String Args[])

每次程序尝试循环时,都会出现并突出显示错误“java.lang.stringindexoutofboundsexception”

ki=选择字符(0)

有人知道为什么会这样吗?。我是编程新手,这让我很困惑。谢谢你的帮助。这个问题的任何解决方案都将是惊人的

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');

该错误表示索引“0”超出了字符串的范围。这意味着用户没有输入任何内容,例如启动程序并按enter键时的情况。要解决此问题,只需添加以下代码行:

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

让我知道这是否有帮助

正如您所指出的,问题在于:

choice = k.nextLine();  
ki= choice.charAt(0);
从文档中:“将此扫描仪移过当前行并返回跳过的输入。”

因此,如果用户按下“回车”键,扫描仪将转到下一行并返回空字符串

为了避免它,只需检查
choice
是否不是空字符串:

if (!"".equals(choice)) {
    // handle ki
    ki= choice.charAt(0);
}
试试这个:
您的问题是扫描仪(k)每次循环重新开始时都需要重置它

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');
    }
}

报告异常时,请准确地复制(到问题中)完整的异常消息。还要至少复制“异常回溯”的前10行左右。