Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming question. I have the following program which requires user inpu

ID: 3764565 • Letter: J

Question

Java Programming question. I have the following program which requires user input it is working to request the input but it doesn't end it just keeps asking for the input. After you enter the last input it starts over requesting the first input again. I can't figure out what I am missing. It is probably obvious but I just can't see it. I did not write the program it was provided to me to use in an assignment to create a new class.

import java.util.Scanner;
public class RentalDemo
{
   public static void main(String[] args)
   {
      String contractNum;
      int minutes;
      Rental[] rentals = new Rental[3];
      int x;
      for(x = 0; x < rentals.length; ++x)
      {
         contractNum = getContractNumber();
         minutes = getMinutes();
         rentals[x] = new Rental(contractNum, minutes);
         rentals[x].setContactPhone(getPhone());
         rentals[x].setEquipType(getType());
      }
      for(x = 0; x < rentals.length; ++x)
        displayDetails(rentals[x]);
   }
   public static String getContractNumber()
   {
      String num;
      Scanner input = new Scanner(System.in);
      System.out.print(" Enter contract number >> ");
      num = input.nextLine();
      return num;
   }
   public static int getMinutes()
   {
      int minutes;
      final int LOW_MIN = 60;
      final int HIGH_MIN = 7200;
      Scanner input = new Scanner(System.in);
      System.out.print("Enter minutes >> ");
      minutes = input.nextInt();
      while(minutes < LOW_MIN || minutes > HIGH_MIN)
      {
         System.out.println("Time must be between " + LOW_MIN +
            " minutes and " + HIGH_MIN + " minutes");
         System.out.print("Please reenter minutes >> ");
         minutes = input.nextInt();
      }
      return minutes;
   }
   public static int getType()
   {
      int eType;
      Scanner input = new Scanner(System.in);
      System.out.println("Equipment types:");
      for(int x = 0; x < Rental.EQUIP_TYPES.length; ++x)
         System.out.println("   " + x + " " + Rental.EQUIP_TYPES[x]);
      System.out.print("Enter equipment type >> ");
      eType = input.nextInt();
      return eType;
   }
   public static void displayDetails(Rental r)
   {
      System.out.println(" Contract #" + r.getContractNumber());
      System.out.println("For a rental of " + r.getHours() +
         " hours and " + r.getExtraMinutes() +
         " minutes,     at a rate of $" + r.HOUR_RATE +
         " per hour and $1 per minute,     the price is $" + r.getPrice());
      System.out.println("Contact phone number is: " + r.getContactPhone());
      System.out.println("Equipment rented is type #" + r.getEquipType() +
         " " + r.getEquipTypeString());
   }  
   public static Rental getLongerRental(Rental r1, Rental r2)
   {
      Rental longer = new Rental();
      int minutes1;
      int minutes2;
      minutes1 = r1.getHours() * Rental.MINUTES_IN_HOUR + r1.getExtraMinutes();
      minutes2 = r2.getHours() * Rental.MINUTES_IN_HOUR + r2.getExtraMinutes();
      if(minutes1 >= minutes2)
         longer = r1;
      else
         longer = r2;          
      return longer;
   }
   public static String getPhone()
   {
      String phone;
      Scanner input = new Scanner(System.in);
      System.out.print("Enter contact phone number >> ");
      phone = input.nextLine();
      return phone;
   }
}

Explanation / Answer

The methods in this class include:

public static void main(String[] args): Its not reading anything from the user.

public static String getContractNumber(): It will read the string num from the user, and returns it. There is no loop here. It will read the whole line, including spaces.

public static int getMinutes(): This will read the minutes from the user. As an integer. Note that you should enter the minutes within the range 60 - 7200. Unless otherwise, it will repeatedly keep prompting.

public static int getType(): This will read an integer value from the user into the variable eType, and will return it.

public static void displayDetails(Rental r): This is not reading anything from the user. Its just an accessor method.

public static Rental getLongerRental(Rental r1, Rental r2): This function is not reading anything directly from the user. May be its reading indirectly from the Rental class.

public static String getPhone(): This function reads a line of string into the variable phone, and will return.

Theoritically speaking, there is nothing wrong with this code. May be you should provide the code for the class Rental. Possibly there is a problem over there. Please provide the complete code so that I'll be able to trace it.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote