Hi I'm writting this code to prepare electricity for as many users as there are input. I don't got when I compile it always shows something where the "if" start. I need a fresh look please. Thanks

import java.text.*; // format output
import java.util.*; // Scanner class

public class electricBill
{
	public static void main(String[] args)
	{
		DecimalFormat num=new DecimalFormat("$,###.00"); // Create format for name num.
		

		// variable names 
		double amountDue; // amount charge to customer
		double chargeRate; // charge rate depend on kwh used
		double oldMeter; // previous meter reading
		double newMeter; // current meter reading
		double kwhUsed; // the eclectricity used
		int accountNum; //customer account number
		String str; // to hold user name input
		char repeat; // to hold yes or no, 'Y' or 'N'		
		
		System.out.println("This program calculates the "+"customer elecitricity usage "+"and the amount due "+"of Peco electric company.");

		// Initialize Scanner to read from DOS window.
		Scanner input = new Scanner(System.in);

		// Get as many different mortgage payments as the user wants.
		
		do
		{
			// Read numbers from the DOS window.
			System.out.print("Enter the Customer name:	");
			str = input.nextLine();

			System.out.print("Enter the customer account number:	");
			accountNum = input.nextInt();
		
			System.out.print("Enter the new meter reading:	");
			newMeter = input.nextDouble();
			
			System.out.print("Enter the old meter reading:	");
			oldMeter = input.nextDouble();
			
			if(kwhUsed <= 300)
			{
				chargeRate=5.00;
			}
			else if(kwhUsed<= 1000)
			{
				chargeRate= 5.00+(0.03*(kwhUsed-300));
			}
			else if(kwhUsed>= 1001)
			{
				chargeRate= 35.00+(0.02*(kwhUsed-1000));
			}

			// Compute the the current electriciity usage.
			kwhUsed= (newMeter- oldMeter);
			
			// compute the current electricity usgae charge
			amountDue= (kwhUsed*chargeRate);

			// Display the customer information on the DOS window.
			System.out.println(str+", "+"account number "+accountNum+". Last meter reading "+oldMeter+", current meter reading "+newMeter+". The total usage of "+kwhUsed+" kwh used"+"will be charge "+num.format(amountDue)+" for this period.");
			

			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("Good-Bye!");
	}
}

Edited 5 Years Ago by ~s.o.s~: Added code tags; please learn to use them

from first look...
kwhUsed is defined AFTER it was checked??
line 57 should be in-front of IF where you define change rate (line 43)

Thank you Monarchmk,
Now I changed the kwhUsed (line 57) position to stay above the if (line 43), the compile still marked some wrong about the amountDue= (kwhUsed*chargeRate); (line 60).
I then moved it up as well right underneath the kwhUsed line, but it didn't do anything different. See anything wrong? Thanx

Line 13
should be double chargeRate=0;
Error is The local variable chargeRate may not be initialized.
Java loves if you assign default values to variables before you try to use them

And DO NOT move amountDue= (kwhUsed*chargeRate); Its position is correct. Line 60 there it should be. Only kwhUsed in above code is wrong positioned

Here is explanation why i tell you to move

in IF loop you count "chargeRate" with "kwhUsed" in calculation. So it is logical that kwhUsed should have some value (should be calculated) before you enter IF chain.
With "amoundDate". This line should stay below IF loop because it depends from "chargeRate" value that is calculated in IF chain.

Edited 5 Years Ago by monarchmk: n/a

OK,
I'm done with the location of the code. I never know that Java needs us to assign a default (in my case here double chargeRate=0), Thanks. May you explain why the rest of the variables don't need us to assign any default numbers?

My problem now is the loop: after I said yes (y, enter) to continue in the loop, my question appeared weird. Instead of asking each question at the time, it asked 2 continue questions in same line like the last line I pasted below, even I changed the print line System.out.print to System.out.println, sameting still happened but with 2 continue questions on 2 separate lines :

This program calculates the customer elecitricity usage and the amount due of Peco electric company.
Enter the Customer name: Tracy
Enter the customer account number: 1234
Enter the new meter reading: 55.00
Enter the old meter reading: 15.67
Tracy, account number 1234. Last meter reading 15.67, current meter reading 55.0. The total usage of 39.33 kwh used will be charge $196.65 for this period.

Would you like to prepare another bill for the next customer ?
Enter Y for Yes or N for No: y
Enter the Customer name: Enter the customer account number:

About default values...
Only changeRate is enclosed in IF chain. By Java logic, IF condition may neever be satisfied so that variable may be left without value. So to be code safe Java ask for default value.Same is for WHILE, DO, SWITCH... All other variables are left out of any conditions and/or are part of some calculations or input for user, so that is why you need default only on changeRate.

On missed question... line 32 should be str = input.next(); not nextLine...

Edited 5 Years Ago by monarchmk: n/a

Thank you very much Monarchmk,
You're awesome! I understand why now and the problem solved now. Thank you for everything. This is what I have:
This program calculates the customer electricity usage and the amount due to Peco electric company customer.
Enter the Customer name: Tracy
Enter the customer account number: 1234
Enter the new meter reading: 45
Enter the old meter reading: 33
Tracy, account number 1234. Last meter reading 33.0, current meter reading 45.0. The total usage of 12.00 kwh used will be charge $60.00 for this period.

Would you like to prepare another bill for the next customer ?
Enter Y for Yes or N for No: y
Enter the Customer name: Trish
Enter the customer account number: 5678
Enter the new meter reading: 78
Enter the old meter reading: 46
Trish, account number 5678. Last meter reading 46.0, current meter reading 78.0. The total usage of 32.00 kwh used will be charge $160.00 for this period.

Would you like to prepare another bill for the next customer ?
Enter Y for Yes or N for No: n
Good-Bye!

Use a counter variable or a for loop.

Any further questions should go in a new thread of your own. Don't hijack old threads to ask new questions.

Edited 4 Years Ago by Ezzaral

This question has already been answered. Start a new discussion instead.