Hi everybody! I am pretty close to finishing this program. It is supposed to calculate a person's energy use based on their past and current bill. I am stuck on the calculations and else/if statements. Thanks in advance for any feedback.

package craft_week4;
//Import scanner library
import java.util.Scanner;

//Begin Class main
public class main {

    //Begin Main Method
    public static void main(String[] args) {
        
        //New Scanner Object sc
        Scanner sc = new Scanner(System.in);
        
        //Declarations
        int prevRead;
        int currentRead;
   
        //Welcome message
        System.out.print("Welcome to the City Power Bill Calculator!\n");
        
        //User input here
        System.out.print("Please enter your previous meter reading: \n");
        prevRead = sc.nextInt(); //Analyze and store input
        System.out.print("Please enter your current meter reading: \n");
        currentRead = sc.nextInt(); //Analyze and store input
        
        //Conditionals
        //Rate A: For 500 KwHs or less = $0.0809 / KwH
        //Rate B: For 501 to 900 KwHs = $0.091 / KwH
        //Rate C: For greater than 901 KwHs = $0.109 / KwH
        //Utilities Tax is 3.46% regardless of usage.
        calcRateA = (currentRead / .0809);
        calcRateB = (currentRead / .091);
        calcRateC = (currentRead / .109);
        calcTax = (Rate * .0346);
        int calcSubtotal = (Rate + calcTax);
        int calcTotal = (calcTax + Rate);
        int calcUsage = 
        
        //Output derived value
        System.out.printf("Your usage was: \n", + calcUsage);
        if Rate < 500 then multiply by .0809

        System.out.printf("Your rate is: %.4f\n", + calcRateA);
        else if Rate is from 501 to 900 multiply by .091

        System.out.printf("Your rate is: %.4f\n", + calcRateB);
        else Rate > 901 multiply by .109

        System.out.printf("Your rate is: %.4f\n", + calcRateC);

        System.out.printf("Your subtotal will be: %.2f\n", + calcSubtotal);

        System.out.printf("Your taxes are: %.2f\n", + calcTax);

        System.out.printf("Your total bill this month is: %.2f\n", + calcTotal);
        
        //While loop
        //Setting loop count to zero
        int response = 0;
        int loops = 0;
        System.out.print("Calculate a new usage? (-1 to Exit, 0 to Continue)\n");
        while ( response == 0 ){
            if ( loops == 0 ) { 
        //User input
        System.out.print("Please enter your previous meter reading: \n");
        System.out.print("Please enter your current meter reading: \n");
        }
            else if (response == -1){
    //End message
    System.out.print("Thank you for using this program. Goodbye!\n");
    break;
            }
    }
        

    } //End Main Method

} //End Class main

Recommended Answers

All 3 Replies

and ... what exactly are you stuck at?

Hi everybody! I am pretty close to finishing this program. It is supposed to calculate a person's energy use based on their past and current bill. I am stuck on the calculations and else/if statements. Thanks in advance for any feedback.

package craft_week4;
//Import scanner library
import java.util.Scanner;

//Begin Class main
public class main {

    //Begin Main Method
    public static void main(String[] args) {
        
        //New Scanner Object sc
        Scanner sc = new Scanner(System.in);
        
        //Declarations
        int prevRead;
        int currentRead;
   
        //Welcome message
        System.out.print("Welcome to the City Power Bill Calculator!\n");
        
        //User input here
        System.out.print("Please enter your previous meter reading: \n");
        prevRead = sc.nextInt(); //Analyze and store input
        System.out.print("Please enter your current meter reading: \n");
        currentRead = sc.nextInt(); //Analyze and store input
        
        //Conditionals
        //Rate A: For 500 KwHs or less = $0.0809 / KwH
        //Rate B: For 501 to 900 KwHs = $0.091 / KwH
        //Rate C: For greater than 901 KwHs = $0.109 / KwH
        //Utilities Tax is 3.46% regardless of usage.
        calcRateA = (currentRead / .0809);
        calcRateB = (currentRead / .091);
        calcRateC = (currentRead / .109);
        calcTax = (Rate * .0346);
        int calcSubtotal = (Rate + calcTax);
        int calcTotal = (calcTax + Rate);
        int calcUsage = 
        
        //Output derived value
        System.out.printf("Your usage was: \n", + calcUsage);
        if Rate < 500 then multiply by .0809

        System.out.printf("Your rate is: %.4f\n", + calcRateA);
        else if Rate is from 501 to 900 multiply by .091

        System.out.printf("Your rate is: %.4f\n", + calcRateB);
        else Rate > 901 multiply by .109

        System.out.printf("Your rate is: %.4f\n", + calcRateC);

        System.out.printf("Your subtotal will be: %.2f\n", + calcSubtotal);

        System.out.printf("Your taxes are: %.2f\n", + calcTax);

        System.out.printf("Your total bill this month is: %.2f\n", + calcTotal);
        
        //While loop
        //Setting loop count to zero
        int response = 0;
        int loops = 0;
        System.out.print("Calculate a new usage? (-1 to Exit, 0 to Continue)\n");
        while ( response == 0 ){
            if ( loops == 0 ) { 
        //User input
        System.out.print("Please enter your previous meter reading: \n");
        System.out.print("Please enter your current meter reading: \n");
        }
            else if (response == -1){
    //End message
    System.out.print("Thank you for using this program. Goodbye!\n");
    break;
            }
    }
        

    } //End Main Method

} //End Class main

here is the format of an if statement to get you started:

if(expression) {
} else if(expression) {
} else {
}

Not sure what is difficult about it

if Rate < 500 then multiply by .0809

should be just written as

if(Rate < 500){
    calcUsage = calcUsage * 0.0809;
}

bur first you need to sort some errors you have before this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.