0

Not actually sure what's wrong, just that at line 81 my scan object is skipped and then the program continues with no clear attempt to read anything, thoughts on what's wrong? btw working in eclipse

import java.util.*;

public class Hospital
{
//--- Instance Variables
private Patient patient;
private Scanner scan;
private double totalPrivateRoomCharges;
private double totalSemiPrivateRoomCharges;
private double totalWardRoomCharges;
private double totalTelephoneCharges;
private double totalTelevisionCharges;
private double totalReceipts;

//--- Constructors
public Hospital()
{
    scan = new Scanner(System.in);
    totalPrivateRoomCharges = 0;
    totalSemiPrivateRoomCharges = 0;
    totalWardRoomCharges = 0;
    totalTelephoneCharges = 0;
    totalTelevisionCharges = 0;
    totalReceipts = 0;

    mainMenu();
}

//--- Methods
public void mainMenu()
{
    int ans = 0;

    do
    {
        System.out.println("        Community Hospital");
        System.out.println();
        System.out.println("             Main Menu");
        System.out.println();
        System.out.println("1) Enter Patient Billing Information");
        System.out.println("2) Print Daily Summary Report");
        System.out.println("3) Exit");
        System.out.println();
        System.out.print("Selection: ");
        ans = scan.nextInt();
        System.out.println();
        if(ans == 1)
        {
           patientBillingInfo();
        }

        if(ans == 2)
        {
           printSummaryReport();
        }
    }
    while(ans != 3);

}

// precondition: none
// postcondition: displays a menu that allows the user to enter a patient's
//      billing info which includes the patient's name, the number of days the 
//      patient stayed in the hospital, and the type of room 
//     (private, semi-private, ward).
//    Once the patient info is retrieved a patient object is created and a
//      billing report is generated that includes the patient's name, length
//      of stay, and the charges incurred including the bill total.
//    The totals that are used to print the hospitals daily summary report
//      are updated.
public void patientBillingInfo()
{
    String name = "";
    int days = 0;
    String room = "";
    System.out.println("        Community Hospital");
    System.out.println();
    System.out.println("        Patient Billing Query");
    System.out.println();
    System.out.println("Enter Patient Name: ");

here the scan object seems to be completely skipped and dose not read at all just moves to the next line and continues.

    name = scan.nextLine(); //getting skiped
    System.out.println("Enter number of days in Hospital: ");
    days = scan.nextInt();
    System.out.println("Enter Room type(P, S, W): ");
    room = scan.next();
    System.out.println();
    System.out.println();
    if(!((room.equalsIgnoreCase("P")) || (room.equalsIgnoreCase("S")) || (room.equalsIgnoreCase("W"))))
    {
        System.out.println("Incorect room information");
        System.out.println("Please enter P, S, or W for room selection");
        System.out.println();
        patientBillingInfo();
    }
    else
    {
        if(room.equalsIgnoreCase("P"))
            totalPrivateRoomCharges += 1*days;
        if(room.equalsIgnoreCase("S"))
            totalSemiPrivateRoomCharges += 1*days;
        if(room.equalsIgnoreCase("W"))
            totalWardRoomCharges += 1*days;
        totalTelephoneCharges += 1*days;
        totalTelevisionCharges += 1*days;
        patient = new Patient(name, days,room);
    }

    System.out.println("        Community Hospital");
    System.out.println();
    System.out.println("    Patient Billing Statement");
    System.out.println();
    System.out.println("Patient's name: " + patient.getName());
    System.out.println("Number of days in Hospital: " + patient.getDays());
    System.out.println();
    System.out.println("Room charge       $ " + patient.getRoomCharge());
    System.out.println("Telephone charge  $ " + patient.getTelephoneCharge());
    System.out.println("Television charge $ " + patient.getTelevisionCharge());
    System.out.println();
    System.out.println("Total charge      $ " +patient.getTotalCharge());
    System.out.println();
    System.out.println();
    mainMenu();
}

// precondition: none
// postcondition: a summary report is printed that includes the daily total
//     room charges for each type of room, the daily total telephone charges,
//     and the daily total television charges. The report also includes the
//     total receipts for the day.
public void printSummaryReport()
{
    double tPRC = 225.0*totalPrivateRoomCharges;
    double tSPRC = 165.0*totalSemiPrivateRoomCharges;
    double tWRC = 95.0*totalWardRoomCharges;
    double tTpC = 1.75*totalTelephoneCharges;
    double tTvC = 3.50*totalTelevisionCharges;
    double tR = tPRC+tSPRC+tWRC+tTpC+tTvC;
    System.out.println("        Community Hospital");
    System.out.println();
    System.out.println("      Daily Billing Summary");
    System.out.println();
    System.out.println("Room Charges");
    System.out.println("  Private:            $" + tPRC);
    System.out.println("  Semi-Private:       $" + tSPRC);
    System.out.println("  Ward:               $" + tWRC);
    System.out.println("Telephone Charges:    $" + tTpC);
    System.out.println("Telvision Charges:    $" + tTvC);
    System.out.println();
    System.out.println("Total Receipts:       $" + tR);
    System.out.println();
}
}

edit: so it just occurred to me that maybe just maybe the rest of the code that this goes with might be useful dunno why I didn't think about this sooner but here's the rest

the class that actually runs

public class Administrator
{   
   public static void main(String[] args)
   {
      Hospital hospital = new Hospital();
   }
}

and the class that works the patient info

public class Patient
{
//--- Constants
public final double PRIVATE = 225.00;
public final double SEMI    = 165.00;
public final double WARD    = 95.00;
public final double TELEPHONE = 1.75;
public final double TELEVISION = 3.50;

//--- Instance Variables
private String name;
private String roomType;
private int days;
//--- Constructors
public Patient(String n, int d, String rT)
{
    name = n;
    days = d;
    roomType = rT;
}
//--- Methods

// precondition:  none
// postcondition: returns the patient name
public String getName()
{
    return name;
}

// precondition:  none
// postcondition: returns the length of stay in days
public int getDays()
{
    return days;
}

// precondition:  none
// postcondition: returns the room type ("P", "S", "W")
public String getRoomType()
{
    return roomType;
}

// precondition:  none
// postcondition: returns the room charge which is dependant upon
// the room type and the length of stay.
public double getRoomCharge()
{
    double charge = 0;

    if(getRoomType().equalsIgnoreCase("P"))
        charge = 225.00*getDays();
    if(getRoomType().equalsIgnoreCase("S"))
        charge = 165.00*getDays();
    if(getRoomType().equalsIgnoreCase("W"))
        charge = 95.00*getDays();
    return charge;
}

// precondition:  none
// postcondition: returns the telephone charge which is dependant upon
//    the length of stay
public double getTelephoneCharge()
{
    double charge = 1.75*getDays();
    return charge;
}

// precondition:  none
// postcondition: returns the television charge which is dependant upon
//    the length of stay
public double getTelevisionCharge()
{
   double charge = 3.50*getDays();
   return charge;
}

// precondition:  none
// postcondition: returns the total charge which is the sum
//    of the room charge, telephone charge, and television charge.
public double getTotalCharge()
{
    double charge = getRoomCharge()*getTelephoneCharge()+getTelevisionCharge();
    return charge;
}
}

really don't know why it didn't occur to me sooner to do this but whatever live and lern right

Jonny09
  • 43
  • 6
  • 2
    Honnestly, just write a [mcve] instead of this block. This is just a `Scanner.nextLine()` issues, we don't need that much information... and this is not convincing me to read your code. – AxelH Jan 19 '17 at 13:34
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – AxelH Jan 19 '17 at 13:35

2 Answers2

0

You could simply scan a line and then parse it as the integer for Integer values.

so for reading integers instead of

val=scan.nextInt()

you could use

String strVal = scan.nextLine();
try {
val = Integer.parseInt(strVal);
} catch (NumberFormatException e) {
    //maybe try again, or break the code ... or proceed as you wish.
}

this is because the nextInt() does not take the "Enter" key into account, and when you press enter after the nextInt() the int is read into the variable expecting nextInt() and the "Return" Key is accepted by the nextLine() which results in an empty line being read into the variable.

Exception_al
  • 1,029
  • 1
  • 9
  • 21
  • I think I understand but I'm confused as to the purpose of the try/catch...what is the purpose of it? and what do I put after the catch? do I just continue with my code? – Jonny09 Jan 19 '17 at 13:56
  • You have a `try...catch` because if someone enters a character or anything else that is not a number, then your code shouldn't throw an Exception, instead gracefully handle it. You could say in the catch that "You entered an invalid number, please try again (last try) and enter a valid number" and then try again (with another `try...catch`) ... or otherwise simply use a default value if the user enters something invalid. So you could say _"enter a number (on invalid input or no input, default value of '1' will be accepted) : "_ or something – Exception_al Jan 19 '17 at 14:00
  • ah, ok I believe I understand now thank you very much for the help. – Jonny09 Jan 19 '17 at 14:03
0

In public void mainMenu() you need to add scan.nextLine(); after ans = scan.nextInt(); in order to clear the rest of the input buffer.

ans = scan.nextInt();
scan.nextLine();    // <----- Add this line here