1
import java.text.DecimalFormat;
import java.util.Scanner;

public class shippingCharges {

  public static void main(String[] args) {

    double inputWeight, inputDistance;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the weight of the package in Kilograms between 0 - 50 exclusive: ");
    inputWeight = sc.nextDouble();
    while (inputWeight < 0.0 || inputWeight > 50.0) {
      System.out.println("Weight must be between 0 - 50 exclusive. Enter the weight: ");
      inputWeight = sc.nextDouble();
    }

    System.out.println("Enter the distance the package is to be shipped between 10 - 3000 inclusive: ");
    inputDistance = sc.nextDouble();
    while (inputDistance < 0 || inputDistance > 3000) {
      System.out.println("Distance must be between 0 - 3000 exclusive. Enter the distance: ");
      inputDistance = sc.nextDouble();

      // Calling ShippingCost Method
      shippingCost(inputWeight, inputDistance);
    }
  }

  public static void shippingCost(double inputWeight, double inputDistance) {

    // Declaring initial variables needed

    double inputRate, blocks, total;

    // Declaring named constants needed

    final double RATE1 = 1.20, RATE2 = 2.20, RATE3 = 3.80, RATE4 = 4.80;
    final double W1 = 2, W2 = 6, W3 = 10, W4 = 50;
    final int DIS = 200;

    Scanner sc = new Scanner(System.in);
    DecimalFormat df = new DecimalFormat("0.00");
    inputWeight = sc.nextDouble();
    inputRate = sc.nextDouble();
    inputDistance = sc.nextInt();

    // Using an if-else statement to determine the rate of per 200 miles shipped

    if (inputWeight <= W1)
      inputRate = RATE1;
    else if (inputWeight > W1 && W1 <= W2)
      inputRate = RATE2;
    else if (inputWeight > W2 && W2 <= W3)
      inputRate = RATE3;
    else if (inputWeight > W3 && W3 <= W4)
      inputRate = RATE4;

    // Calculating the number of 200 mile blocks using the built-in ceil() method and total

    blocks = Math.ceil(inputDistance / DIS);
    total = blocks * inputRate;

    // Printing the Invoice Summary

    System.out.println("\tThe shipping rate is: " + df.format(inputRate) + " per 200-mile shipped. ");
    System.out.println("\tThere are " + blocks + " 200-mile blocks in " + inputDistance + "miles. ");
    System.out.println("\tTotal = " + blocks + " x " + inputRate + " = " + total);
  }
}

I have been having an issue recently for one of my programming classes. The issue seems pretty simple but I'm just starting out. Basically, I'm trying to call the void method I created properly into my main method program. However, every time I run the program it displays the System.out.println() however terminates the program after I get to the shipping method. Any help would mean a ton, thanks.

2
  • 2
    Your call to shippingCost is in while (inputDistance < 0 || inputDistance > 3000) { (it should not be). Mar 23, 2021 at 3:24
  • you sir are the goat Mar 23, 2021 at 3:51

2 Answers 2

0

changing while to if moving call to shippingCost outside if condition as below, would help

public static void main() {
        System.out.println("Enter the weight of the package in Kilograms between 0 - 50 exclusive: ");
        
        Scanner sc = new Scanner(System.in);
        double inputWeight = sc.nextDouble();
        if (inputWeight < 0.0 || inputWeight > 50.0) {
            System.out.println("Weight must be between 0 - 50 exclusive. Enter the weight: ");
            inputWeight = sc.nextDouble();
        }

        System.out.println("Enter the distance the package is to be shipped between 10 - 3000 inclusive: ");
        double inputDistance = sc.nextDouble();
        if (inputDistance < 0 || inputDistance > 3000) { 
            System.out.println("Distance must be between 0 - 3000 exclusive. Enter the distance: ");
            inputDistance = sc.nextDouble();
        }

            //Calling ShippingCost Method 
            shippingCost(inputWeight, inputDistance);
    }
0

When you reach the shippingCost Method, you are again taking user input for:

inputWeight = sc.nextDouble();
inputRate = sc.nextDouble();
inputDistance = sc.nextInt();

You don't need to do that, since you are already getting these values as input to the method itself (check the method signature/parameters). Just remove the above code sniplet from your shippingCost Method, and then your code will work just fine.

Also, since you are not getting input for inputRate variable, Just initialize the following variable, right after its declaration, inside shippingCost Method:

inputRate = 0.0;

Regards.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.