codenewb
codenewb

Reputation: 101

variable may not be initialized error and general suggestions

I'm extremely new to all of this, my first year majoring in programming and web design. I take my courses online and my Professor is occasionally hard to get a hold of.

Anyways I have written this code from a program that takes banking info (account number, account balance and account type [savings or checkings], calculates interest and service charge and outputs the account number and account balance.

My main problem is a syntax error stating that my variable "servCharge" might not have been initialized. I've looked over this a numerous times and can not track down what I have done wrong.

I'm sure something like this is easy to spot to most of you. Any help with this and overall suggestions about how I can clean up my code would be helpful and greatly appreciated!

Here is what I have so far:

import java.util.*;

public class Unit5
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {

      final double CHECKING_MIN_BAL = 25.00;
      final double CHECKING_SFEE = 25.00;
      final double MAX_CHECKING_INTR = .015;
      final double MIN_CHECKING_INTR = .03;
      final double SAVINGS_MIN_BAL = 500.00;
      final double SAVINGS_SFEE = 10.00;
      final double SAVINGS_INTR = .04;
      final double FIVE_THOU = 5000.00;

      String accountNumber;
      String accountBal;
      String accountType;
      double accountBalDbl;
      double interest;
      double servCharge;
      double accountBalFinal;

      System.out.println("Bank");
      System.out.println("Please enter a 10 digit "
                           +"account number or enter # to exit: ");
      while (! ( accountNumber = console.next() ).equalsIgnoreCase( "#" ) ) 
      {
        if (accountNumber.matches("^[0-9]{10}"))
        {
          System.out.println();
        }else{
          System.out.println("Invalid account number!");
          System.exit(0); 
        }

        System.out.println("Please enter account balance: ");
        accountBal = console.next();
        {
          if (accountBal.matches("^\\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(\\.[0-9][0-9])?$"))  //adjust so it takes more numbers before decimal
          {
             System.out.println();
          }else{
             System.out.println("Invalid dollar amount!");
             System.exit(0);
          }

          System.out.println("Please enter account type,"
                        +"C for checking or S for savings");
          accountType = console.next();
          {
          if (accountType.matches("^[Ss | Cc]"))  //look into this
          {
             System.out.println();
          }else{
             System.out.println("Invalid account type, please enter"
                          +"C or S");
             System.exit(0);
          }

          accountBalDbl = Double.parseDouble(accountBal);
          {
             if (accountType == ("[Cc]"))
             {     
                if (accountBalDbl < CHECKING_MIN_BAL)
                   servCharge = CHECKING_SFEE;
                else
                   servCharge = 0.0;

             if (accountBalDbl < CHECKING_MIN_BAL + FIVE_THOU)
                interest = accountBalDbl * MIN_CHECKING_INTR;
             }
          }
          {
           if (accountType == ("S | s"))
             {
             if (accountBalDbl < SAVINGS_MIN_BAL)
                servCharge = SAVINGS_SFEE;
                interest = 0.0;
             }else{ 
                servCharge = 0.0;
                interest = accountBalDbl * SAVINGS_INTR;
             }
          }
          accountBalFinal = (accountBalDbl - servCharge) + interest;
          System.out.println("Account Number: " + accountNumber);
          System.out.println();
          System.out.println("Current Balance: " + accountBalFinal);
          System.out.println();
          System.out.println("Please enter a 10 digit "
                     +"account number or enter # to exit: ");

         }
        }
      }
    }
  }

Upvotes: 1

Views: 89

Answers (2)

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

The error itself suggests how to overcome this problem. You need to initialize the variable to some default value. This is because local variables (variables declared inside a method) are not initialized implicitly in java. Hence, compiler will prompt you with an error message saying initialize the variable before using it.

You wont get the error message if you just declare one(without initializing it) inside the method body, and never use it. Compiler will only bother you, if you try to use an uninitialized local variable in some way like assigning it a value, printing its contents etc.

I suggest you to read the java basics to get hold of these things. Hope this helps.

Upvotes: 1

Joey
Joey

Reputation: 464

Your formatting is a little tricky to decipher, but the error you are mentioning has to do with the fact that there is at least one branch through your code where you don't assign a value to servCharge. That is a problem when you try to do this:

accountBalFinal = (accountBalDbl - servCharge) + interest;

Make sure you've always set servCharge to something through all your branches, and/or make sure you set it to a default value at the beginning of your code. Something like:

servCharge = 0.0;

The big problem with that is that you might end up with a value for servCharge you weren't expecting (0.0). Check all your if/else branches and make sure you are assigning the appropriate value to servCharge in each branch.

Upvotes: 1

Related Questions