简体   繁体   English

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

[英]Java.Lang.Stringindexoutofboundsexception index out of range (0)

each time the program tries to loop, the error "java.lang.stringindexoutofboundsexception" comes up and highlights 程序每次尝试循环时,都会出现错误“ java.lang.stringindexoutofboundsexception”并突出显示

ki=choice.charAt(0); 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. 该错误表示索引“ 0”超出了字符串的范围。 This means the user typed in no input, such as the case when you start the program and hit the enter key. 这意味着用户没有输入任何内容,例如启动程序并按enter键的情况。 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." 在docs nextLine()中 :“使该扫描器前进到当前行之外,并返回被跳过的输入。”

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: 为了避免这种情况,只需检查choice是否不是空字符串:

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. 您的问题是扫描仪(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');
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java错误java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Java Error java.lang.StringIndexOutOfBoundsException: String index out of range: 0 “ main” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:17 - “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 17 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: - java.lang.StringIndexOutOfBoundsException: String index out of range: java.lang.StringIndexOutOfBoundsException:yuicompressor中的字符串索引超出范围 - java.lang.StringIndexOutOfBoundsException: String index out of range in yuicompressor java.lang.StringIndexOutOfBoundsException:字符串索引超出范围 - java.lang.StringIndexOutOfBoundsException: String index out of range java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:45 - java.lang.StringIndexOutOfBoundsException: String index out of range: 45 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1 - java.lang.StringIndexOutOfBoundsException: String index out of range: 1 “异常:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围” - “Exception:java.lang.StringIndexOutOfBoundsException: String index out of range” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - java.lang.StringIndexOutOfBoundsException: String index out of range: 4 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:3 - java.lang.StringIndexOutOfBoundsException: String index out of range: 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM