0

I am getting these 3 errors after compiling .. Pls help and thnx in advance..

  1. error: variable ac might not have been initialized tcws= cm+tm+ac+am+ta;
  2. error: variable am might not have been initialized tcws= cm+tm+ac+am+ta;
  3. error: variable ta might not have been initialized tcws= cm+tm+ac+am+ta;

My code:

import java.lang.*; import java.util.Scanner; class CellPhoneBill { public static void main(String[]args) { double cm ,tm ,ac, am, ta, tcws, tax, taxpluscost ; Scanner s=new Scanner(System.in); System.out.print("Enter the number of minutes and also enter the number of text messages "); cm= s.nextDouble(); tm= s.nextDouble(); if(cm<=50&& tm<=50) { System.out.print("Base charge= $15.00"); } else if(cm>50 && tm>50) { System.out.println("Enter no of additional call minutes (if any) and additional messages(if any) "); ac=s.nextDouble(); am= s.nextDouble(); cm=0.25* ac; am=0.15*am; System.out.println("Additional call charges = " +ac); System.out.println("Additional message charges = " +am); ta=ac+am; System.out.println("Total Additional charges = " +ta); } else if(cm>=0&& tm>=0) { System.out.println("911 Call charges =$0.44" ); } else { tcws= cm+tm+ac+am+ta; System.out.println("Total cost without Sales Tax ="+tcws); tax=( 5/100.00*(tcws)); System.out.println(" 5% Sales Tax ="+tax); taxpluscost=tcws+tax; System.out.println("Total cost Including 5% Sales Tax ="+taxpluscost); } } } 
4
  • What part of "variable might not have been initialized" don't you understand? Commented May 21, 2017 at 15:52
  • I am getting these 3 errors after compiling tcws= cm+tm+ac+am+ta in this line whereas i have declared there type(error showing up for variables ac,am,ta .. there type is double declared at the start) .. Why is this error coming up?? can't get it .. pls help Commented May 21, 2017 at 15:55
  • Just initialize your variables like : double cm = 0.0 Commented May 21, 2017 at 16:11
  • Possible duplicate of Java: Error: variable might not have been initialized Commented May 21, 2017 at 18:21

4 Answers 4

1

It means that the variables ac, am and ta have not been assigned any value. You cannot sum variables that haven’t got any value.

For the fix, it depends on what you are trying to obtain. Maybe you need to do ac=s.nextDouble(); as in some of the other cases to read a value from the user. Or maybe you just need to do ac = 1.0; or whatever the appropriate value would be. And the same for the other two variables, of course.

Possibly you intended a different if-else structure where the values assigned to the three variables in one if branch should also be used in the last else part?

Sign up to request clarification or add additional context in comments.

Comments

0

For Auto initialization you can declare them in your class as instance variable instead in static method.You can use the same variable in your static method then.

Comments

0

Your scanner only prompts the user for two values initially, neither of which are ac or am. Thusac = s.nextDouble() and am = s.nextDouble () aren't actually assigning values to ac and am

Comments

0

In java references to primitive data types are stored in the same register as the variable.

double a = 12.345; 

Initializes a variable a and sets it to the value of 12.345.

You must initialize your variables prior to using them lest you get an error.

edited for corrections

1 Comment

@OleV.V. Thanks, edited post for corrections. Here's a good source: stackoverflow.com/questions/29854959/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.