| | |
Java program using scanner
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
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);
}
}
}
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);
}
}
}
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
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.
He did give you a suggestion on how to fix the error. Do you understand his recommendation?
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
•
•
•
•
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.
(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
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.)
I've only been exposed to Java programming since Jan 07, so I'm a true noob & idiot at programming...
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
Here is a sample of your code:
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.
Java Syntax (Toggle Plain Text)
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- put the Dos 's command into java program? (Java)
- Running java command thru java program (Java)
- Help with Java program writing (Java)
- Java program sample for getting Systeminfo (Java)
- JAVA program help (Student/Grades) (Java)
- Help with a java program home work assignment (Java)
Other Threads in the Java Forum
- Previous Thread: Java run time enviorment
- Next Thread: GlassPane
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads transforms tree ui web windows






