I have been trying to get my program to run for a few days now. I have gotten it almost completed I think, but keep getting "error: variable checkFee might not have been initialized" on line 37.

import java.util.Scanner;  // scanner class

public class PROB3_CHAL15
public static void main(String[] args)
    double checks =0,
           totalfee =0,
           fee = 10,
           fee1 =.1,
           fee2 = .08,
           fee3 = .06,
           fee4 = .04,
    String input;
    Scanner keyboard = new Scanner(System.in);

    int number; 
    System.out.println("Please enter the number of checks you wrote for the past month");
    input = keyboard.nextLine();

    if (checks < 0)  // negative is false
    System.out.print("ERROR: You cannot enter a negative amount!");

    if(checks >= 60)
        checkFee =(fee4);
    else if(checks >= 40)
        checkFee = (fee3);
    else if(checks >=20)
        checkFee = (fee2);
    else if(checks <20)
        checkFee = (fee1);

                     //adds all fees
    totalfee = (fee + checkFee * checks);

                     //if closing true(because user didn't enter negative number, show total fees
    System.out.print("Your total monthly Bank Fees are $" + totalfee);

This is what I have so far. Also, the original question is as follows:

A bank charges a base fee of $10 per month, plus the following check fees for a commercial checking account:
$.10 each for less than 20 checks
$.08 each for 20-39 checks
$.06 each for 40-59 checks
$.04 each for 60 or more checks

Write a program that asks for the number of checks written for the month. The programs should then calculate and display the bank's service fees for the month.

checkfee is a local variable in main. Local variables have to be initialized with a value, because they have no default value. Human logic dictates that the conditions in lines 27 to 34 satisfy the entire domain of possible values of checks, meaning one of the if or else if statements will execute every time, ergo checkFee will be initialized. However, the java compiler doesn't check those conditions in that same way, and will always assume that there's a possibility that none of them will execute. It always looks for a default value, or a case that covers the anything else condition.

You can either initialize checkFee before hand, by changing line 14 to checkFee=0; or any other float value, or change line 33 else if to simply else, which satisfies the java compiler's condition of anything else.

This article has been dead for over six months. Start a new discussion instead.