You MUST explicitly initialise all local variables in a method before reading them for the first time.
You don't do that, so you get an error.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Thanks for replying, as I stated in my post, I'm a NOOB at this & I posted this to see if anybody could give me a suggestion on how to fix this error.
No, judging from this post, you came here for someone to give you the code to complete and functional program. Well, I'm sorry (no I'm not), but that is not how it works. Read jwenting's post again, consider his recommendation, study your code, and attempt to fix it. Surely, you have learned how to initialise variables.
(His response "No I haven't, and stop calling me Shirley!" Both halves of that sentence as senseless as the other.)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Here is a sample of your code:
public static void main (String[] args) {
String inputSuite;
int nightsA; // Here you declare, but do not define nightsA
...
inputSuite = keyboard.nextLine();
if (inputSuite.equalsIgnoreCase("A")) {
System.out.println("How many nights do you plan on staying at the R & R?");
nightsA = keyboard.nextInt(); // Here you define nightsA
} else {
System.out.println("Try one of our other suites. They might suit you better. ");
}
/* if the last if statement has not as yet
* evaluated as true nightsA is not defined here
* the compiler realizes this and flags it as an
* error. Change the line where you declare
* nightsA to also make it a declaration. i.e.
* int nightsA = 0;
* Or, you can set nightsA in the else side of
* the above if statement.
*/
if (nightsA <= 5 || nightsA >=1) { suiteApricenoDCst = (suiteA * stateTax);
suiteApricenoDCft = (suiteA * federalTax);
suiteApricenoDC = (suiteA + suiteApricenoDCst + suiteApricenoDCft * nightsA); // or here
System.out.println("Your price for your stay is " + suiteApricenoDC);
} else if (nightsA > 5) { // or here
suiteADC = (suiteA * discount);
suiteApriceDCst = (suiteADC * stateTax);
suiteApriceDCft = (suiteADC * federalTax);
suiteApriceDC = (suiteADC + suiteApricenoDCst + suiteApricenoDCft * nightsA);
System.out.println("Your price for your stay is " + suiteApricenoDC);
} else {
System.out.println("ERROR!!!");
}
...
}
Like jwenting said, you must explicitly declare all local variables. That means, before your code attempts to do anything with it, it must have appeared on the left side of an equals sign, in such a way so that that line will have executed before anything else is done with the variable.
And, you must do the same sort of thing with every other variable you have declared inside of this method.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Why resurrect a zombie to inadequately answer an already adequately answered question?
Closing this thread.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494