Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: I'm so lost

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I'm so lost

    I'm new to Java and in the book I'm reading, Java Programming by Joyce Farrell, things have gotten very complicated very quickly. I've spent well over ten hours on this problem and think I've butchered it to the point of no return. The problem is to create a delivery class that puts the year, invoice number, code (1 for long distance delivery and 2 for overnight), and a weight. Based on the weight, it determines the fee. Finally, using a boolean I have to determine if the person wants overnight delivery (an extra 35) or not (no extra charge).

    I made a lot more progress than I thought I ever would but I'm still completely stuck on how to tie it all together, especially how to show the displayFee in the main and get it to print out something understandable. I genuinely do not know what to do and every minor correction I make leads to a bunch of errors and it not compiling. Can someone please help me?

    This is what I have for the class:

    public class Delivery
    {
      int year;
      int delNum;
      double weight;
      int code;
      boolean day = false;
      boolean regular = false;
      double fee; 
      boolean overNight = true;
     
     
      public Delivery(int year, int delNum, int code, double weight, boolean overNight)//constructor for data.
      {
         this.year = year;//year and delNum will combine for delivery number.
         this.delNum = delNum;
         this.code = code;//must be (1) or (2)
         this.weight = weight;
         this.overNight = overNight;   
     
         if(overNight == true) {
         System.out.println("it's true");
         }
         else {
         System.out.println("it's false");
         }         
      }
      public String toString()
      {
       String info =  "Delivery year = " + year + " Delivery Number = " + delNum +  
        " code = " + code + " weight = " + weight + 
        "Charge for overnight delivery is:" + overNight;
        return info;  
      }
     
     
      public double displayFee(double weight)//used to calculate fees with if...else if loop(s).
      {
         double fee = 0.0;
     
       if(code == 1)
       {
          if(weight < 5)
          {
             fee = 12;
          }
          else if(weight >= 5 && weight <= 20)
          {
             fee = 16.50;
          }
          else if(weight > 20)
          {
            fee = 22.00;
          }
       else if(code == 2)
       {
          if(weight < 5)
          {
             fee = 35.00;
          }
          else if(weight >= 5)
          { 
             fee = 47.95;
          }
        }
        }
        return fee;
      }
     
      public void displayFee(int year, int delNum, int code, double weight)//calls methods
      {
      System.out.println("Year is: " + year);
      System.out.println("Delivery number is: " + delNum);
      System.out.println("Delivery code is: " + code);
      System.out.println("Weight is: " + weight);
      }
    }

    The main:

    import java.util.Scanner;
     
    public class CreateDelivery
    {
      public static void main(String[] args)
      {
      int year;
      int delNum;
      double weight;
      int code;
      double fee;
      double displayFee;
      boolean overNight = true;
     
     
      Scanner input = new Scanner(System.in);
      System.out.println("Hi!  What is the current year?");
      year = input.nextInt();
     
      while(year < 2001 || year > 2025)
      {
          System.out.println("You have entered an invalid year.");
          System.out.println("Please select a year between 2001 and 2025.");
          year = input.nextInt();
      }
     
      System.out.println("Please enter your four digit delivery number");
      delNum = input.nextInt();
     
      while(delNum < 1 || delNum > 9999)
      {
          System.out.println("You have entered an invalid delivery number.");
          System.out.println("Please select a delivery number between 1 and 9999.");
          delNum = input.nextInt();
      }
       System.out.println("Please enter your code.");
       code = input.nextInt();
     
      while(code < 1 || code > 2)
      {
          System.out.println("You have entered an invalid code number.");
          System.out.println("Please select 1 for local delivery or 2 for long distance.");
          code = input.nextInt();
      }
      System.out.println("Please enter the weight of your package");
      weight = input.nextInt();
     
      while(weight < .10 || weight > 100)
      {
          System.out.println("You have entered an invalid weight");
          System.out.println("Please select a weight between .10 pounds and 100 pounds.");
          weight = input.nextDouble();
      }
       System.out.println("Please enter 1 for overnight delivery or 2 for regular");
       overNight = input.nextBoolean();
     
      if(overNight == true){
       System.out.println("This will add $35 to your total.");}
       else if(overNight == false){
       System.out.println("This will add no additional charges.");
       }
     
      Delivery firstDelivery = new Delivery(year, delNum, code, weight, overNight);
      System.out.println(firstDelivery.toString());
      }
      {
     }
     }

    And this is the ouput (I have no idea what it means or how to correct it):

    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:909)
    	at java.util.Scanner.next(Scanner.java:1530)
    	at java.util.Scanner.nextBoolean(Scanner.java:1825)
    	at CreateDelivery.main(CreateDelivery.java:65)
     
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm so lost

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextBoolean(Scanner.java:1825)
    at CreateDelivery.main(CreateDelivery.java:65)
    The user's input does not match what the nextBoolean() method on line 65 was expecting.
    Either have the user enter correct input for the nextBoolean() method
    or change the method used to match what the user wants to enter. The instructions say to enter a 1 or 2. Those are integers not booleans.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm so lost

    I fixed it so now it will say "true" (that they picked over night delivery) but how do I add the $35 to the total? And, how do I generate the "fee" from the class? Is the whole displayFee part useless? I don't know how to make a line/code to add everything together for a total price.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm so lost

    how do I add the $35 to the total
      total += 35;  //  add 35 to the total
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm so lost

    I add that to the if...else in the main?

    Do you happen to know how to use the fees generated from the if...else statements in the class? Nothing I do seems to work.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm so lost

    Can you explain how the fees are to be calculated and where they should be added into the total?
    Don't work on the code until you understand the process and what steps need to be taken just like you would do it with paper and pencil. When you have the steps worked out, then work on the code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm so lost

    I've gotten everything to work except on the if...else statements. If the user inputs "2" it always comes back with a fee of 0. I've tried changing various things but don't know what else I can do.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I'm so lost

    Post your updated code. A sample run copied from the console would also be helpful.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm so lost

    what else I can do.
    Try debugging the code. Add some println statements that print out the values of the variables as the code executes so you can see where the code is executing and how the variables' values are changed. Print out the user's input after it is entered and then its value at other places in the code and also print out the value of fee as it is changed. You need to see why the code is not changing the value of fee.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Have I lost it?
    By pooleman133 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 7th, 2012, 09:22 PM
  2. Help, please. I'm lost
    By Borb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 17th, 2010, 08:50 PM
  3. I'm lost
    By stoptheerrors in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 31st, 2010, 08:47 AM
  4. Im lost....
    By brale76578 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 31st, 2010, 08:09 AM
  5. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM