0

I have to write a program that calculates the shipping cost for something being sent in the mail.

Here are some specifications and what a practice run should look like:

Calculations:

Use: Ship Method Cost Overnight $5 * weight Two Day $2 * weight Economy $1 * weight (yes, that's just = weight) Data Validation On this program, you'll also need to perform some validation in the main method. Specifically:

The item description must not be empty. The item weight must be > 0. The shipping method must be either O, T or E. The program should accept either lower or uppercase equivalents. Sample Runs: Be sure to end your output with a println.

Run #1: No item description entered:

Enter item description: <-- user hits enter key but doesn't enter item description Invalid description. Program cannot continue

Run #2: Invalid item weight entered:

Enter item description: A big box <-- user enters valid description Enter item weight in lbs: -5 <-- user enters invalid weight Invalid shipping weight. Program cannot continue

Run #3: Invalid shipping method entered:

Enter item description: A big box Enter item weight in lbs: 3.5

How fast would you like to ship your package: (O)vernight (T)wo Days (E)conomy (may take up to 7 days) Choose an option: P Invalid shipping method. Program cannot continue

Run #4: Invoice Generated

Enter item description: A big box Enter item weight in lbs: 3.5

How fast would you like to ship your package: (O)vernight (T)wo Days (E)conomy (may take up to 7 days) Choose an option: o

*** WE SHIP INVOICE *** Item Description: A big box Item Weight: 3.50 Ship Method: O Total Cost: $17.50

Decomposition You must use the decomposition provided below. For maximum sanity, remember to build and test one method at a time in development mode. Two of these methods have already been written for you and supplied as part of you code template for this exercise.

Use: double getWallHeight(Scanner scnr) - This method is called from the main method. It accepts the scanner as a parameter, prompts the user for the wall height and returns the value entered by the user to the main method.

Use: double getWallWidth(Scanner scnr) - This method is called from the main method. It accepts the scanner as a parameter, prompts the user for the wall height and returns the value entered by the user to the main method.

Use: double calcWallArea(double wallHeight, double wallWidth) - This method is called from the main method. It accepts the wallHeight and wallWidth and calculates the wall area in square feet.

Use: double calcGallons(double wallArea) - This method is called from the main method. It accepts the wall area and determines how much paint is need to paint the entire room.

Use: void displayResults(double wallArea, double gallonsPaintNeeded) - This method is called from the main method. It accepts the wallArea and the amount of paint needed to paint the room. It displays the results to the user including the walk area and the number of paint cans needed to be purchased as an integer.

Here is my code:

import java.util.*;
import java.util.Scanner; 

public class WeShipIt {

public static final int OVERNIGHT_CHARGE = 5;
public static final int TWO_DAY_CHARGE = 2;
public static final int ECONOMY_CHARGE = 1;

public static void main(String[] args){
  Scanner keyboard = new Scanner(System.in);  //scanner object to pass around
  double weight;
  String itemDescription;
  char shipMethod;
  double shippingCost;
    
  itemDescription = getitemDescription(keyboard);
  weight = getShipWeight(keyboard);
  shipMethod = getShipClass(keyboard);
  shippingCost = calculateShipping(shipMethod, weight);
  
  
  if (itemDescription.length() == 0){
     
     System.out.println("Invalid description. Program cannot continue");
     System.exit(0);
  } else {
     getShipWeight(keyboard);
     
     if (weight <= 0){
     
     System.out.println("Invalid shipping weight. Program cannot continue");

     } else {
     getShipClass(keyboard);
     
      if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')){
     System.out.println("Invalid shipping method. Program cannot continue");
     
     } else {
     displayResults(itemDescription, weight, shipMethod, shippingCost);
     }
     }
     }
}
//get item description
  public static String getitemDescription(Scanner keyboard){
      
  System.out.println("Enter item description:");
  String itemDescription = keyboard.next();

  return itemDescription;
  }

//get item weight
public static double getShipWeight(Scanner console){
       
  System.out.println("Enter item weight in lbs:");
  double itemWeight = console.nextDouble();

  return itemWeight;
}

 //get user's choice for shipping method and return it
 public static char getShipClass(Scanner keyboard) {
  char shipMethod;

  //get shipping method
  System.out.println();
  System.out.println("How would you like to ship your package:");
  System.out.println("(O)vernight");
  System.out.println("(T)wo Days");
  System.out.println("(E)conomy (may take up to 7 days)");
  System.out.println("Choose an option: ");
  shipMethod = keyboard.next().charAt(0); //Prof. Dan says leave this line in here. Will explain in 
 class.
  shipMethod = Character.toUpperCase(shipMethod);
  
  return shipMethod;
}

 //calculate and return shipping charge
 public static double calculateShipping(char shipMethod, double weight){
  double shipCharge;
  
  if (shipMethod == 'O') {
  shipCharge = weight * OVERNIGHT_CHARGE;
  } 
  else if (shipMethod == 'T') {
  shipCharge = weight * TWO_DAY_CHARGE;
  } 
  else if (shipMethod == 'E') {
  shipCharge = weight * ECONOMY_CHARGE;
  } else {
  shipCharge = 0;
  }
  return shipCharge;
}

 //display shipping charge invoice
 public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double 
 shipCost) {
  System.out.println();
  System.out.println("*** WE SHIP INVOICE ****");
  System.out.println("Item Description: " + itemDescription);
  System.out.printf("Item Weight: %.2f\n" + shipWeight);
  System.out.println("Ship Method: " + shipMethod);
  System.out.printf("Total Cost: $%.2f\n" + shipCost);
 }
}

This is the output when I use "box 3.5 0" as my input:

Enter item description: Enter item weight in lbs:

How would you like to ship your package: (O)vernight (T)wo Days (E)conomy (may take up to 7 days) Choose an option: Enter item weight in lbs:

I don't have to reprint out the input's given, but I am very confused why it asks for the item's weight again and doesn't printout the final result.

I am also getting this error:

Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at WeShipIt.getShipWeight(WeShipIt.java:67)
at WeShipIt.main(WeShipIt.java:35)

I have tried for hours to fix this on my own, but I am asking for help as a last resort. I am new to computer science and probably made some very dumb mistake. Thank you

  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – PM 77-1 Sep 25 '20 at 23:54

3 Answers3

0

You have actually called the "getShipWeight(keyboard)" and "getShipClass(keyboard)" in the else block and that's why it is asking for weight again. Just make these statements comment and your code will run properly.

0

Good you are learning java.

There is a slight change that needs to be done in our program. getShipWeight & getShipClass are called twice in our actual code.

please use this code

import java.util.Scanner;

public class WeShipIt {

    public static final int OVERNIGHT_CHARGE = 5;
    public static final int TWO_DAY_CHARGE = 2;
    public static final int ECONOMY_CHARGE = 1;

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in); // scanner object to pass around
        double weight;
        String itemDescription;
        char shipMethod;
        double shippingCost;

        itemDescription = getitemDescription(keyboard);
        /*weight = getShipWeight(keyboard);
        shipMethod = getShipClass(keyboard);
        shippingCost = calculateShipping(shipMethod, weight);*/

        if (itemDescription.length() == 0) {

            System.out.println("Invalid description. Program cannot continue");
            System.exit(0);
        } else {
            weight = getShipWeight(keyboard);

            if (weight <= 0) {

                System.out.println("Invalid shipping weight. Program cannot continue");

            } else {
                shipMethod =getShipClass(keyboard);
                shippingCost = calculateShipping(shipMethod, weight);

                if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')) {
                    System.out.println("Invalid shipping method. Program cannot continue");

                } else {
                    displayResults(itemDescription, weight, shipMethod, shippingCost);
                }
            }
        }
    }

//get item description
    public static String getitemDescription(Scanner keyboard) {

        System.out.println("Enter item description:");
        String itemDescription = keyboard.next();

        return itemDescription;
    }

//get item weight
    public static double getShipWeight(Scanner console) {

        System.out.println("Enter item weight in lbs:");
        double itemWeight = console.nextDouble();
        return itemWeight;
    }

    // get user's choice for shipping method and return it
    public static char getShipClass(Scanner keyboard) {
        char shipMethod;

        // get shipping method
        System.out.println();
        System.out.println("How would you like to ship your package:");
        System.out.println("(O)vernight");
        System.out.println("(T)wo Days");
        System.out.println("(E)conomy (may take up to 7 days)");
        System.out.println("Choose an option: ");
        shipMethod = keyboard.next().charAt(0); // Prof. Dan says leave this line in here. Will explain in class.
        shipMethod = Character.toUpperCase(shipMethod);

        return shipMethod;
    }

    // calculate and return shipping charge
    public static double calculateShipping(char shipMethod, double weight) {
        double shipCharge;

        if (shipMethod == 'O') {
            shipCharge = weight * OVERNIGHT_CHARGE;
        } else if (shipMethod == 'T') {
            shipCharge = weight * TWO_DAY_CHARGE;
        } else if (shipMethod == 'E') {
            shipCharge = weight * ECONOMY_CHARGE;
        } else {
            shipCharge = 0;
        }
        return shipCharge;
    }

    // display shipping charge invoice
    public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double shipCost) {
        System.out.println();
        System.out.println("*** WE SHIP INVOICE ****");
        System.out.println("Item Description: " + itemDescription);
        System.out.println("Item Weight: "+ shipWeight);
        System.out.println("Ship Method: " + shipMethod);
        System.out.println("Total Cost: $"+ shipCost);
    }
}
Sri
  • 344
  • 1
  • 3
  • 13
0

The code you showed has 2 errors which are as :

1: System.out.printf("Item Weight: %.2f\n"+ shipWeight); and
System.out.printf("Total Cost: $%.2f\n"+ shipCost);
Doing this will throw a MissingFormatArgumentException
Cause when using printf and declaring an argument it adjusts itself with the first variable (value to be printed) passed to it after comma. and in this case, there ain't any comma so it won't recognize and throw a MissingFormatArgumentException.

Instead, you should write like this:

System.out.printf("Item Weight: %.2f\n",shipWeight);
System.out.printf("Total Cost: $%.2f\n", shipCost);

2: You are calling getShipWeight(keyboard) and getShipClass(keyboard) twice.
First time, when the user just enters the program.
Second time, in the else statement.
Which forces you to entre weight and shipping method again.

Here are the final changes:

public static final int OVERNIGHT_CHARGE = 5;
    public static final int TWO_DAY_CHARGE = 2;
    public static final int ECONOMY_CHARGE = 1;

    public static void main(String[] args){
      Scanner keyboard = new Scanner(System.in);  //scanner object to pass around
      double weight;
      String itemDescription;
      char shipMethod;
      double shippingCost;
        
      itemDescription = getitemDescription(keyboard);
      weight = getShipWeight(keyboard);
      shipMethod = getShipClass(keyboard);
      shippingCost = calculateShipping(shipMethod, weight);
      
      
      if (itemDescription.length() == 0){
         
         System.out.println("Invalid description. Program cannot continue");
         System.exit(0);
      } else {
          // getShipWeight(keyboard); <-- this is forcing uset to enter weight again. 
         if (weight <= 0){
         
         System.out.println("Invalid shipping weight. Program cannot continue");

         } else {
           // getShipClass(keyboard); <--  this is forcing user to again give shipping method
          if (!(shipMethod == 'O' || shipMethod == 'T' || shipMethod == 'E')){
         System.out.println("Invalid shipping method. Program cannot continue");
         
         } else {
         displayResults(itemDescription, weight, shipMethod, shippingCost);
         }
         }
         }
    }
    //get item description
      public static String getitemDescription(Scanner keyboard){
          
      System.out.println("Enter item description:");
      String itemDescription = keyboard.next();

      return itemDescription;
      }

    //get item weight
    public static double getShipWeight(Scanner console){
           
      System.out.println("Enter item weight in lbs:");
      double itemWeight = console.nextDouble();

      return itemWeight;
    }

     //get user's choice for shipping method and return it
     public static char getShipClass(Scanner keyboard) {
      char shipMethod;

      //get shipping method
      System.out.println();
      System.out.println("How would you like to ship your package:");
      System.out.println("(O)vernight");
      System.out.println("(T)wo Days");
      System.out.println("(E)conomy (may take up to 7 days)");
      System.out.println("Choose an option: ");
      shipMethod = keyboard.next().charAt(0); //Prof. Dan says leave this line in here. Will explain in 
      shipMethod = Character.toUpperCase(shipMethod);
      
      return shipMethod;
    }

     //calculate and return shipping charge
     public static double calculateShipping(char shipMethod, double weight){
      double shipCharge;
      
      if (shipMethod == 'O') {
      shipCharge = weight * OVERNIGHT_CHARGE;
      } 
      else if (shipMethod == 'T') {
      shipCharge = weight * TWO_DAY_CHARGE;
      } 
      else if (shipMethod == 'E') {
      shipCharge = weight * ECONOMY_CHARGE;
      } else {
      shipCharge = 0;
      }
      return shipCharge;
    }

     //display shipping charge invoice
     public static void displayResults(String itemDescription, double shipWeight, char shipMethod, double 
     shipCost) {
      System.out.println();
      System.out.println("*** WE SHIP INVOICE ****");
      System.out.println("Item Description: " + itemDescription);
      System.out.printf("Item Weight: %.2f\n",shipWeight); // <-- changed
      System.out.println("Ship Method: " + shipMethod);
      System.out.printf("Total Cost: $%.2f\n", shipCost);  // <-- changed
     }

Output:

Enter item description:
box
Enter item weight in lbs:
3.5

How would you like to ship your package:
(O)vernight
(T)wo Days
(E)conomy (may take up to 7 days)
Choose an option: 
O

*** WE SHIP INVOICE ****
Item Description: box
Item Weight: 3.50
Ship Method: O
Total Cost: $17.50
Swapnil Padaya
  • 624
  • 3
  • 13