Question

It is saying that my local variable newaccbalance may not have been initialized. I know I declared it as a double. Help please

 import java.util.*;

public class Pg244Problem12 {

  public static void main(String[] args) 
  { 

   int accnum, minbalance, currentbalance;
   int acctype;
   double newaccbalance;

   Scanner console = new Scanner(System.in);



   System.out.println("Enter the customer's account number:");
   accnum = console.nextInt();
   System.out.println("Enter the customer's account type by using the number 1 for Checking or 2 for Savings:");
   acctype = console.nextInt();
   System.out.println("Enter the minimum balance the customer's account can have:");
   minbalance = console.nextInt();
   System.out.println("Enter the current balance of the customer's account:");
   currentbalance = console.nextInt();



   // Checkings
    if(acctype == 1 && currentbalance >= (minbalance+5000)){
     newaccbalance = ((currentbalance*.05)*(1/12));
   }
    if (acctype == 1 && currentbalance >= minbalance && currentbalance <  (minbalance+5000)){
     newaccbalance = ((currentbalance*.03)*(1/12)); 
    }
    if (acctype == 1 && currentbalance < minbalance){
     newaccbalance = (currentbalance-25);
  }

   // Savings
    if (acctype == 2 && currentbalance >= minbalance){
      newaccbalance = ((currentbalance*.04)*(1/12));
    }
    if (acctype == 2 && currentbalance < minbalance){
      newaccbalance = (currentbalance - 10);
    }



    System.out.println("The account number is: "+ accnum);
    System.out.println("The account type is: "+ acctype);
    System.out.println("The current balance is: "+ currentbalance);
    System.out.println("The new account balance is: "+ newaccbalance);

  }
}
Was it helpful?

Solution

First of all, declaring and initializing is not the same thing.

double newaccbalance; declares the variable.

newaccbalance = 42; is initializing the variable.

The problem in your code is that the compiler can not guarantee that any of your if-statements will be true, therefore it is possible for newaccbalance to be left uninitialized.

I suggest two things:

First of all, initialize the variable to a default value, double newaccbalance = 0; will both declare and initialize the variable.

Secondly, change the structure of your if-statements and also use if-else-if, something like this:

if (acctype == 1) {
    // For these if statements, acctype is 1 so we don't need to check that again
    if(currentbalance >= (minbalance+5000)){
        newaccbalance = ((currentbalance*.05)*(1/12));
    }
    else if (currentbalance >= minbalance) {
        //  && currentbalance <  (minbalance+5000) will be true because the above if-statement is **not** true
        newaccbalance = ((currentbalance*.03)*(1/12)); 
    }
    else { 
        // if (acctype == 1 && currentbalance < minbalance) would always be true here
        newaccbalance = (currentbalance-25);
    }
}
else if (acctype == 2){
     // Savings
     if (currentbalance >= minbalance) {
          newaccbalance = ((currentbalance*.04)*(1/12));
     }
     else { // currentbalance < minbalance) is always true here
          newaccbalance = (currentbalance - 10);
     }
}
else {
     // acctype is neither 1 or 2, what should we do now? RuntimeError, Catastrophic failure, the monsters are coming! We're screwed!
}

OTHER TIPS

You are declaring your variable. You need to initialize your variable.

Declaring is where you create the variable:

double newaccbalance;

Initializing is where you assign a variable a value:

newaccbalance = 0;

So what you need to do is:

double newaccbalance = 0.0;

All your assignments are wrapped in if control structures. If none of the conditions is evaluated to true, the variable will remain unassigned. As a local variable it does not get a default value either.

That is why the message says that it may be not initialized.

It is not initialized. When you declare it try double newaccbalance = 0.0;

I think the problem is newaccbalance is only set conditionally (in the if statements) so it can never be guaranteed to be set to a value.

Initializing and declaring are two different things.

I don't think you're getting an error, but a warning.
Regardless, Java is correct.
Your variable newaccbalance may not have been initialized.

You've declared it a double, but you only assign it a value inside if statements.
Java does not know whether those if statements cover all possible cases and therefore warns you what newaccbalance may in fact be unassigned.

Note that Java does not assign a zero value to undefined variables.
You have to do that yourself.

Change the top declaration to :

double newaccbalance = 0;  //Or whatever default value you want.

Either that or add an extra else behind the last one like so:

else if (acctype == 2 && currentbalance < minbalance){
  newaccbalance = (currentbalance - 10);
}
else { newaccbalance = 0; }

This will ensure to the compilers satisfaction the newaccbalance has a defined value, rather than a random one.
You should always ensure that this is the case and kuddo's for heeding the warning and taking action.
Undefined variables can be a source of very hard to track down bugs, because often the value turns out some reasonable value except in 1% of cases. Because every run of the code is different, it can be difficult to reproduce the bug, let alone diagnose it.

This is why Java is insistent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top