943,931 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 29196
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2007
0

Java program using scanner

Expand Post »
I'm a real Noob to programming & am taking a class of intro to Java.
I keep getting a variable not initializing error on the nightsA line calculation.
I'm going to warn once again I'm a NOOB to this, so any help is appreciated.

import java.util.Scanner;

public class SunnyDaze

{

public static void main (String[] args)
{
String inputSuite;
int nightsA;
int nightsB;
int nightsC;
int suiteA = 150;
int suiteB = 175;
int suiteC = 200;
double stateTax = 0.045;
double federalTax = 0.05;
double discount = 0.1;
double suiteApricenoDCst;
double suiteApricenoDCft;
double suiteApricenoDC;
double suiteADC;
double suiteApriceDCst;
double suiteApriceDCft;
double suiteApriceDC;
double suiteBpricenoDCst;
double suiteBpricenoDCft;
double suiteBpricenoDC;
double suiteBDC;
double suiteBpriceDCst;
double suiteBpriceDCft;
double suiteBpriceDC;
double suiteCpricenoDCst;
double suiteCpricenoDCft;
double suiteCpricenoDC;
double suiteCDC;
double suiteCpriceDCst;
double suiteCpriceDCft;
double suiteCpriceDC;


Scanner keyboard = new Scanner(System.in);

System.out.println("Which is the letter of the suite you want to stay at? (A, B, or C?)");
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();
}
else
{
System.out.println("Try one of our other suites. They might suit you better. ");
}
if (nightsA <= 5 || nightsA >=1)
{
suiteApricenoDCst = (suiteA * stateTax);
suiteApricenoDCft = (suiteA * federalTax);
suiteApricenoDC = (suiteA + suiteApricenoDCst + suiteApricenoDCft * nightsA);
System.out.println("Your price for your stay is " + suiteApricenoDC);
}
else
if (nightsA > 5)
{
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!!!");
}

if (inputSuite.equalsIgnoreCase("B"))
{
System.out.println("How many nights do you plan on staying at the Pair O'Dice?");
nightsB = keyboard.nextInt();
}
else
{
System.out.println("If this doesn't suit you, then try another suite, plz");
}
if (nightsB >= 1 || nightsB <= 5)
{
suiteApricenoDCst = (suiteB * stateTax);
suiteApricenoDCft = (suiteB * federalTax);
suiteApricenoDC = (suiteB + suiteBpricenoDCst + suiteBpricenoDCft * nightsB);
System.out.println("Your price for your stay is " + suiteBpricenoDC);
}
else
if (nightsB > 5)
{
suiteBDC = (suiteB * discount);
suiteBpriceDCst = (suiteBDC * stateTax);
suiteBpriceDCft = (suiteBDC * federalTax);
suiteBpriceDC = (suiteBDC + suiteBpricenoDCst + suiteBpricenoDCft * nightsB);
System.out.println("Your price for your stay is " + suiteBpricenoDC);
}

if (inputSuite.equalsIgnoreCase("C"))
{
System.out.println("How many nights do you plan to stay in our luxurous Luckheart Manor?");
nightsC = keyboard.nextInt();
}
else
{
System.out.print("I don't know what else to do for you, sorry...");
}
if (nightsC >= 1 || nightsC <= 5)
{
suiteCpricenoDCst = (suiteC * stateTax);
suiteCpricenoDCft = (suiteC * federalTax);
suiteCpricenoDC = (suiteC + suiteCpricenoDCst + suiteCpricenoDCft * nightsC);
System.out.println("Your price for your stay is " + suiteCpricenoDC);
}
else
if (nightsC >= 5)
{
suiteCDC = (suiteC * discount);
suiteCpriceDCst = (suiteCDC * stateTax);
suiteCpriceDCft = (suiteCDC * federalTax);
suiteCpriceDC = (suiteCDC + suiteCpricenoDCst + suiteCpricenoDCft * nightsC);
System.out.println("Your price for your stay is " + suiteApricenoDC);
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PCKing is offline Offline
6 posts
since Mar 2007
Mar 4th, 2007
0

Re: Java program using scanner

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 4th, 2007
0

Re: Java program using scanner

Click to Expand / Collapse  Quote originally posted by jwenting ...
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.
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PCKing is offline Offline
6 posts
since Mar 2007
Mar 4th, 2007
0

Re: Java program using scanner

He did give you a suggestion on how to fix the error. Do you understand his recommendation?
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Mar 4th, 2007
0

Re: Java program using scanner

Click to Expand / Collapse  Quote originally posted by Dukane ...
He did give you a suggestion on how to fix the error. Do you understand his recommendation?
No, I'm a NOOB!!!

Sry, bout this...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PCKing is offline Offline
6 posts
since Mar 2007
Mar 4th, 2007
0

Re: Java program using scanner

Click to Expand / Collapse  Quote originally posted by PCKing ...
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.)
Last edited by masijade; Mar 4th, 2007 at 10:26 am. Reason: typo
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 4th, 2007
0

Re: Java program using scanner

Click to Expand / Collapse  Quote originally posted by masijade ...
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.)
That's the error that I get & That's what I don't understand, especially within (if, else statements when calculating a price).

I've only been exposed to Java programming since Jan 07, so I'm a true noob & idiot at programming...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PCKing is offline Offline
6 posts
since Mar 2007
Mar 4th, 2007
0

Re: Java program using scanner

Here, let me explain my train of thought in this program.

I'm trying to calculate a price including taxes up to 5 nights staying at a hotel.

so I use

define variables

ask suite letter then go into (if, else mode)

if (1st letter chosen)
then with letter chosen, do math with & without discount including taxes (which also means i need a range of numbers 1-4 & 5 > are the 2 sets which means i need another embedded if, else???)
else
if (2nd letter chosen)
etc.
else
if(3rd letter chosen)
else
error

end of story.

I just need to know how to do this w/Scanner.
Last edited by PCKing; Mar 4th, 2007 at 10:46 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PCKing is offline Offline
6 posts
since Mar 2007
Mar 5th, 2007
0

Re: Java program using scanner

Here is a sample of your code:
Java Syntax (Toggle Plain Text)
  1. public static void main (String[] args) {
  2. String inputSuite;
  3. int nightsA; // Here you declare, but do not define nightsA
  4.  
  5. ...
  6.  
  7. inputSuite = keyboard.nextLine();
  8.  
  9. if (inputSuite.equalsIgnoreCase("A")) {
  10. System.out.println("How many nights do you plan on staying at the R & R?");
  11. nightsA = keyboard.nextInt(); // Here you define nightsA
  12. } else {
  13. System.out.println("Try one of our other suites. They might suit you better. ");
  14. }
  15. /* if the last if statement has not as yet
  16.   * evaluated as true nightsA is not defined here
  17.   * the compiler realizes this and flags it as an
  18.   * error. Change the line where you declare
  19.   * nightsA to also make it a declaration. i.e.
  20.   * int nightsA = 0;
  21.   * Or, you can set nightsA in the else side of
  22.   * the above if statement.
  23.   */
  24. if (nightsA <= 5 || nightsA >=1) { suiteApricenoDCst = (suiteA * stateTax);
  25. suiteApricenoDCft = (suiteA * federalTax);
  26. suiteApricenoDC = (suiteA + suiteApricenoDCst + suiteApricenoDCft * nightsA); // or here
  27. System.out.println("Your price for your stay is " + suiteApricenoDC);
  28. } else if (nightsA > 5) { // or here
  29. suiteADC = (suiteA * discount);
  30. suiteApriceDCst = (suiteADC * stateTax);
  31. suiteApriceDCft = (suiteADC * federalTax);
  32. suiteApriceDC = (suiteADC + suiteApricenoDCst + suiteApricenoDCft * nightsA);
  33. System.out.println("Your price for your stay is " + suiteApricenoDC);
  34. } else {
  35. System.out.println("ERROR!!!");
  36. }
  37.  
  38. ...
  39.  
  40. }

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Mar 6th, 2007
0

Re: Java program using scanner

Thanks for answering my question.
Error fixed!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
PCKing is offline Offline
6 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: please reply me if any one knows about this ans
Next Thread in Java Forum Timeline: JAVA / MySQL View iteration





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC