当我尝试从用户收集字符串输入并将其转换为字符类型时,我总是收到超出范围的错误。这里是我的代码: 编辑:我的问题没有得到回答。我在我的charAt语句中添加了一个参数,但它仍然不起作用。当我运行它时,它告诉我问题出现在第33行。这个问题不是我得到的任何链接的重复。我无法理解这个所谓的类似问题。请问有人能告诉我第33行有什么问题吗?Java - 字符串索引超出范围错误
//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;
}
}
}
而且这将是有益的,如果你能帮助我编写的代码,将在结束方式在下面的评论呈现的显示计算: –
,什么是错误堆栈跟踪出现“超出范围错误”的位置? –
[String index out of range]可能重复(http://stackoverflow.com/questions/5011866/string-index-out-of-range) – paisanco