2016-10-02 104 views
-2

当我尝试从用户收集字符串输入并将其转换为字符类型时,我总是收到超出范围的错误。这里是我的代码: 编辑:我的问题没有得到回答。我在我的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; 
     } 
    } 
} 
+0

而且这将是有益的,如果你能帮助我编写的代码,将在结束方式在下面的评论呈现的显示计算: –

+1

,什么是错误堆栈跟踪出现“超出范围错误”的位置? –

+3

[String index out of range]可能重复(http://stackoverflow.com/questions/5011866/string-index-out-of-range) – paisanco

回答

1

其实我在代码部分发现了一些问题。

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

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

另一个问题是,你没有给区域分配任何字符,而是你应该通过想要的字符在.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 characterinput拿,你可以把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; 
     } 
    } 
} 
+0

OMG谢谢你sooo! –

+0

它现在的作品!谢谢Sanket Makani –

+0

没问题的队友! –