Java - 字符串索引超出范围错误

时间:2016-10-02 16:10:56

标签: java

当我尝试从用户收集字符串输入并将其转换为char类型时,我不断出现超出范围的错误。这是我的代码: 编辑:我的问题尚未得到解答。我将一个参数放入我的charAt语句中,但它仍然无法正常工作。当我运行它时,它告诉我问题在第33行。这个问题不是我得到的链接的重复。我无法理解与此相似的问题。有人可以告诉我第33行有什么问题吗?

//Arthur Fidas
import java.util.Scanner;   //Needed for Scanner class
import java.lang.String;
/**
    This program computes and displays the shipping charges for Fed Ex.
*/

public class FedEx
{
    public static void main (String[] args)
    {
        final double ZONE_A = .6, ZONE_B = 1.0, ZONE_C = 1.25, ZONE_D = 1.4;
        String input;
        char zone;
        String name;
        double weight;
        double total = 0.0;
        int serviceCode;
        int hazardCode;
        double hazardCodeCalculation;

        //Create a Scanner object for keyboard input
        Scanner keyboard = new Scanner (System.in);

        //Prompt user for package weight
        System.out.println("What is the weight of the package?: ");
        weight = keyboard.nextDouble();

        //Prompt user for Zone
        System.out.print("Enter the zone letter: ");
        input = keyboard.nextLine();
        zone = input.charAt(0);

        //Calculate zone price
        switch(zone)
        {
            case 'a':
            case 'A':
                total = weight*ZONE_A;
                break;
            case 'b':
            case 'B':
                total = weight*ZONE_B;
                break;
            case 'c':
            case 'C':
                total = weight*ZONE_C;
                break;
            case 'd':
            case 'D':
                total = weight*ZONE_D;
                break;
            default:
                System.out.println("Please enter A, B, C, or D.");
                break;
        }

        //Prompt user for service charge
        System.out.println("Enter the Special Service number: ");
        serviceCode = keyboard.nextInt();

        //Caculate Service Charge
        if (serviceCode == 1)
        {
            total += 0;
        }
        else if (serviceCode == 2)
        {
            total += 10;
        }
        else if (serviceCode == 3)
        {
            total += 25;
        }
        else
        {
            System.out.println("Please enter 1, 2, or 3.");
        }
        //Prompt user for Hazard Code
        System.out.println("Enter the Hazard Code number :");
        hazardCode = keyboard.nextInt();

        //Calculate Hazard Charge
        switch(hazardCode)
        {
            case 1:
                total += 0;
                break;
            case 2:
                hazardCodeCalculation = total * .1;
                total += hazardCodeCalculation;
                break;
            case 3:
                hazardCodeCalculation = total * .25;
                total += hazardCodeCalculation;
                break;
            default:
                System.out.println("Please enter either 1, 2, or 3.");
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

实际上我在这段代码中发现了一些问题。

 //Prompt user for Zone
        System.out.print("Enter the zone letter: ");
        input = keyboard.nextLine();
        zone = input.charAt();

首先,您使用.nextLine()方法进行字符串输入,这可能会产生问题。有关此内容的更多信息,请参阅this

另一个问题是你没有将任何角色分配到区域,而是应该在index方法中传递.charAt()所需的角色。

因此,您可以two方式执行此操作,以防您想要输入String的第一个字符。对于另一个字符,请更改.charAt()方法中的索引值。

1)

//Prompt user for Zone
    System.out.print("Enter the zone letter: ");
    keyboard.nextLine();  //Storing that Enter key Input (Garbage value)
    input = keyboard.nextLine();
    zone = input.charAt(0);

2)

//Prompt user for Zone
    System.out.print("Enter the zone letter: ");
    input = keyboard.next();  //In case your String input doesn't contain Space in between.
    zone = input.charAt(0);

如果您只想one character作为input,则可以character输入而不是String

//Prompt user for Zone
    System.out.print("Enter the zone letter: ");
    zone = keyboard.next().charAt(0);

更正代码

//Arthur Fidas
import java.util.Scanner;   //Needed for Scanner class
import java.lang.String;
/**
    This program computes and displays the shipping charges for Fed Ex.
*/

public class FedEx
{
    public static void main (String[] args)
    {
        final double ZONE_A = .6, ZONE_B = 1.0, ZONE_C = 1.25, ZONE_D = 1.4;
        String input;
        char zone;
        String name;
        double weight;
        double total = 0.0;
        int serviceCode;
        int hazardCode;
        double hazardCodeCalculation;

        //Create a Scanner object for keyboard input
        Scanner keyboard = new Scanner (System.in);

        //Prompt user for package weight
        System.out.println("What is the weight of the package?: ");
        weight = keyboard.nextDouble();

        /*THIS FOLLOWING PORTION CAN BE CHANGED WITH PROVIDED SOLUTIONS, TRY ANY OF THEM*/

        //Prompt user for Zone                             
        System.out.print("Enter the zone letter: ");
        input = keyboard.next();
        zone = input.charAt(0);

        /*TILL THIS*/

        //Calculate zone price
        switch(zone)
        {
            case 'a':
            case 'A':
                total = weight*ZONE_A;
                break;
            case 'b':
            case 'B':
                total = weight*ZONE_B;
                break;
            case 'c':
            case 'C':
                total = weight*ZONE_C;
                break;
            case 'd':
            case 'D':
                total = weight*ZONE_D;
                break;
            default:
                System.out.println("Please enter A, B, C, or D.");
                break;
        }

        //Prompt user for service charge
        System.out.println("Enter the Special Service number: ");
        serviceCode = keyboard.nextInt();

        //Caculate Service Charge
        if (serviceCode == 1)
        {
            total += 0;
        }
        else if (serviceCode == 2)
        {
            total += 10;
        }
        else if (serviceCode == 3)
        {
            total += 25;
        }
        else
        {
            System.out.println("Please enter 1, 2, or 3.");
        }
        //Prompt user for Hazard Code
        System.out.println("Enter the Hazard Code number :");
        hazardCode = keyboard.nextInt();

        //Calculate Hazard Charge
        switch(hazardCode)
        {
            case 1:
                total += 0;
                break;
            case 2:
                hazardCodeCalculation = total * .1;
                total += hazardCodeCalculation;
                break;
            case 3:
                hazardCodeCalculation = total * .25;
                total += hazardCodeCalculation;
                break;
            default:
                System.out.println("Please enter either 1, 2, or 3.");
                break;
        }
    }
}