-2

I keep getting an out of range error when I try to collect string input from the user and convert it to a char type. Here is my code: Edit: My question hasn't been answered. I put a parameter into my charAt statement but it's still not working. When I run it, it tells me that the problem is on line 33. This question question isn't a duplicate of whatever the link I got was. I can't make sense of the supposedly similar question to this one. Can someone just please tell me what's wrong with line 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;
        }
    }
}
8
  • Also it would be helpful if you could help me write code that would display the calculations at the end in the way presented in the comment below: Commented Oct 2, 2016 at 16:14
  • 1
    and what is the error stacktrace where you are getting out of range error? Commented Oct 2, 2016 at 16:14
  • 3
    Possible duplicate of String index out of range Commented Oct 2, 2016 at 16:16
  • The error is coming from the part of the code where i ask for the zone letter and use the input variable for the user's input. and then try to convert the input variable into char using the zone variable Commented Oct 2, 2016 at 16:17
  • You're calling String#charAt without a parameter, I don't think some of this would even compile. Also for reading inputs, its best to either String#equalsIgnoreCase or, in the case of you using switch, to switch on something like Character#toLowerCase of the input. Commented Oct 2, 2016 at 16:18

1 Answer 1

1

Actually I found some problems in this code part.

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

First you are taking String input using .nextLine() method which may create problem. For more Information about this, Refer this.

Another problem is that you are not allocating any character to zone rather you should pass index of desiring character in .charAt() method.

So you can do this in two ways in case you want first character of entered String.For another character, change index value in .charAt() method.

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

If you want to take only one character as an input, You can take character input instead of String.

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

Corrected Code

//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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ArttyFidasJr.you should accept the answer if it helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.