0

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);

  }
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111

5 Answers5

3

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!
}
Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • Give your variable some default value when you declare it. – ArniDat Oct 12 '13 at 20:45
  • Either have `else` parts where this variable is assigned as well, or have it initialized during the declaration. See the above answers. – PM 77-1 Oct 12 '13 at 20:46
  • okay so i did what you said but its still not running that variable through the if statements. it just simply prints out 0 – user2874840 Oct 12 '13 at 20:49
  • i have done that also and it still says it has not been initialized. im not sure how to get the variable to go through the if statements and print out what its new value is – user2874840 Oct 12 '13 at 20:53
  • @user2874840 See my updated answer. If the "variable is not running through the if-statements" it might be because acc-type is neither 1 or 2. – Simon Forsberg Oct 12 '13 at 20:55
  • well i input the acctype so it was one or the other – user2874840 Oct 12 '13 at 20:56
1

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;
Troubleshoot
  • 1,816
  • 1
  • 12
  • 19
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.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
0

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.

Harley
  • 26
  • 4
0

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.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • i did that but it still doesnt run that variable through the if statements and return a new value it just says it is zero still – user2874840 Oct 12 '13 at 21:09