Question by Imconfused: Java Sourcecode is said to have some variables not initialized.?
import TerminalIO.KeyboardReader;
public class AutoIns{
public static void main (String [] args) {
KeyboardReader reader = new KeyboardReader();
int zip = 1905;
int sdip,yrsExp=0;
int carValue;
double compulsoryTot = 500.00;
double collisionTot,comprehensiveTot,bodilyInjuryMax,zipTot,driversEdTot,sdipTot,yrsExpTot,airBagTot,antiTheftTot=0.0;
String Name;
double totIns,carYear,carBrand,carModel,airbags,antitheft,collisionIns,comprehensiveIns,bodilyInjuryIns,yes,bodyInjuryIns,driversEd;
System.out.println(“Please Enter Your Name”);
Name = reader.readLine();
System.out.println (“What is your car value?”);
carValue = reader.readInt();
System.out.println(“If you want Collision Insurance type 1 if not enter any key”);
collisionIns = reader.readDouble();
if (collisionIns ==1)
collisionTot = carValue * .04;
System.out.println(“If you want Comprehensive Insurance type 1 if not enter any key”);
comprehensiveIns = reader.readDouble();
if (comprehensiveIns==1)
comprehensiveTot=carValue*.03;
System.out.println(“If you want Bodily Injury Insurance type 1 if not enter any key”);
bodyInjuryIns = reader.readDouble();
if (bodyInjuryIns==1)
bodilyInjuryMax = 200.00;
System.out.println(“Please enter your zip code”);
zip = reader.readInt();
if (zip==1905)
zipTot=carValue*.03;
System.out.println(“If you passed Driver’s Education course please type 1 if not enter any key”);
driversEd = reader.readDouble();
if (driversEd==1)
driversEdTot=-(carValue*.02);
System.out.println(“Please Enter Your SDIP Total”);
sdip = reader.readInt();
if (sdip>0)
sdipTot=sdip*100.00;
else
sdipTot= -200.00;
System.out.println(“Please Enter Your Years Exp Total”);
yrsExp = reader.readInt();
if (yrsExp >=5)
yrsExpTot = -100.00;
System.out.println(“If ou have front seat air bags please type 1 if not enter any key”);
airbags = reader.readDouble();
if (airbags==1)
airBagTot= -200.00;
System.out.println(“If you have a car alarm please enter 1 if not enter any key”);
antitheft = reader.readDouble();
if (antitheft==1)
antiTheftTot=-(carValue*.01);
totIns = compulsoryTot+collisionTot+comprehensiveTot+bodilyInjuryMax+zipTot+driversEdTot+sdipTot+yrsExpTot+airBagTot+antiTheftTot;
System.out.println(“Your total auto insurance is: “);
System.out.println(totIns);
reader.pause();
}
}
I have no idea what I did wrong in the program I spent hours trying to figure it out.
Best answer:
Answer by ealhmund27
Initialize String Name. So, instead of having String Name; put String Name = “”;
Also, as an aside, I know Java initializes integers and doubles to 0, it’s generally a good idea to explicitly initialize them to zero when you first declare them. Makes for more uniform/better looking code.
Answer by Mose Schrute
it could be one of the following vars:
totIns,carYear,carBrand,collisionTot,comprehensiveTot,bodilyInju,carModel,airbags,carValue,Name
don’t use a variable’s value if it has none yet.
Know better? Leave your own answer in the comments!
1 comments:
Post a Comment