Can anyone help me on this Java program I have? it's just about a do...while and it's driving me nuts. I got it to compile and work on the first run, but when I need to repeat the program is where I run into a slight problem. It's supposed to not bunch up in the repeat. here is what I am talking about when I repeat the output:
Would you like to prepare another bill for the next customer?
Enter Y for Yes or N for No: Y
State your company name: Please enter customer name: <--- this should be two lines.
My code:
Would you like to prepare another bill for the next customer?
Enter Y for Yes or N for No: Y
State your company name: Please enter customer name: <--- this should be two lines.
My code:
Code:
import java.text.DecimalFormat; // for decimal formatted output
import java.util.Scanner; // package needed for GUI
public class Bill
{
public static void main(String[] args) throws Exception
{
DecimalFormat num = new DecimalFormat("$,###.00");
// variables
String Company; // to hold Company input
String str; // to hold Customer
int AccountNumber; // to hold AccountNumber
char repeat; // to hold yes or no, 'Y' or 'N'
double kwhUsed; // kilowatts used per hour
double OldMeterReading; // string for inputting OldMeterReading
double NewMeterReading; // string for inputting NewMeterReading
double AmountDue;
double charge = 0.0;
Scanner input = new Scanner(System.in); // initialize scanner
do
{
System.out.print("State your company name: "); // Company name
Company = input.nextLine();
System.out.print("Please enter customer name: "); // CustomerName
str = input.nextLine();
System.out.print("Enter account number: "); // AccountNumber
AccountNumber = input.nextInt();
System.out.print("What is your old meter reading? "); // OldMeterReading);
OldMeterReading = input.nextDouble();
System.out.print("What is your new meter reading? "); // NewMeterReading);
NewMeterReading = input.nextDouble();
// calculate the current electricity usage
kwhUsed = (NewMeterReading - OldMeterReading);
// computations
if(kwhUsed <= 300)
{
charge = 5.00;
}
else if(kwhUsed <= 1000)
{
charge = 5.00 +(0.03*(kwhUsed - 300));
}
else if (kwhUsed >= 1001);
{
charge = 35.00 +(0.02*(kwhUsed - 1000));
}
AmountDue = (charge*kwhUsed); // calculate the bill
System.out.println("Company name is: " + Company); // company name
System.out.println("Customer name is: " + str); // customer
System.out.println("Account number is: " + AccountNumber); // account number
System.out.println("Old Meter Reading is: " + OldMeterReading); // old meter rating
System.out.println("New Meter Reading is: " + NewMeterReading); // new meter rating
System.out.println("Kilowatts per hour used is: " + kwhUsed); // kilowatts per hour
System.out.println("Amount Due is: " + num.format(AmountDue)); // amount due
System.out.println(); //Prints a blank line.
// As many users as there are input
System.out.println("Would you like to prepare another bill "+"for the next customer?");
System.out.print("Enter Y for Yes or N for No: ");
str=input.next(); // Read next char
repeat=str.charAt(0); // Get the first char.
}
while (repeat=='Y' || repeat=='y');
System.out.println("Thank you. Goodbye.");
}
}
Last edited by a moderator: