Results 1 to 10 of 10
- 04-01-2011, 02:09 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Help with Simple If-Else problem!
Getting this Error when I try to compile.
Trying to Compile this:Java Code:----jGRASP exec: javac -g EvergreenTrees.java EvergreenTrees.java:56: variable singleTree might not have been initialized treeCost = numTrees * singleTree; ^ EvergreenTrees.java:72: variable deliveryCost might not have been initialized totalCharges = treeCost + deliveryCost; ^ 2 errors
Anyone know what I'm doing wrong? =/Java Code:import java.util.Scanner; import java.text.DecimalFormat; public class EvergreenTrees { public static void main(String[] args) { DecimalFormat formatter = new DecimalFormat("#,##0.00"); Scanner keyboard = new Scanner(System.in); int forDelivery; int numTrees; int treeHeight; double singleTree; double treeCost; double deliveryCost; double totalCharges; System.out.println("How many trees are you purchasing?"); numTrees = keyboard.nextInt(); System.out.println("What is the height of the tree you would like to buy in Feet? (Whole numbers only)"); treeHeight = keyboard.nextInt(); if(treeHeight < 3) { singleTree = 39.00; } else if(treeHeight > 3 && treeHeight < 5) { singleTree = 69.50; } else if(treeHeight > 6 && treeHeight < 8) { singleTree = 99.00; } else if(treeHeight > 8) { singleTree = 199.50; } else System.out.println("You entered an invalid height."); treeCost = numTrees * singleTree; System.out.println("Will the trees be delivered? Enter 1 for Yes, Enter 0 for No."); forDelivery = keyboard.nextInt(); if(numTrees < 5) { deliveryCost = 10.00 * numTrees; } else if(numTrees >= 5) { deliveryCost = 50.00; } else System.out.println("You entered an invalid answer."); totalCharges = treeCost + deliveryCost; } }
- 04-01-2011, 02:15 AM #2
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
Declaring variables does not default them to any value if you declare them within a method.
Either declare your variables outside any method, or initialize them to a specific value.
- 04-01-2011, 02:18 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Sorry if I sound dumb lol, I'm new to this whole Java thing.
But didn't I declare it outside the method up top? where I put
double singleTree;
and
double deliveryCost; ?
err so lost >.> how would I make this work?
treeCost = numTrees * singleTree;
totalCharges = treeCost + deliveryCost;
- 04-01-2011, 02:20 AM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 04-01-2011, 02:26 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
- 04-01-2011, 02:29 AM #6
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
Java doesn't check to make sure every possible condition is accounted for. If, somehow, none of the conditions were satisfied, the final "else" will run and your singleTree variable remains un-initialized (as does deliveryCost).Java Code:if(treeHeight < 3) { singleTree = 39.00; } else if(treeHeight > 3 && treeHeight < 5) { singleTree = 69.50; } else if(treeHeight > 6 && treeHeight < 8) { singleTree = 99.00; } else if(treeHeight > 8) { singleTree = 199.50; } else System.out.println("You entered an invalid height.");
As Solarsonic said, either declare these variables outside of any method (at the moment, you have them declared in the 'main' method) or initialize them with a default value.
Edit: It is simply compiler semantics. singleTree will change to whatever amount it it supposed to based on user input once it compiles.
- 04-01-2011, 02:32 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
- 04-01-2011, 02:35 AM #8
- 04-01-2011, 02:35 AM #9
Where you declare them.Ah, okay so where in my whole script would be a good place to Initialize singleTree and deliveryCost to 0?
- 04-01-2011, 02:37 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Very simple problem
By Andyj in forum Java CertificationReplies: 3Last Post: 09-17-2010, 06:49 AM -
Need help with simple problem
By newb_tewb in forum New To JavaReplies: 3Last Post: 04-08-2010, 02:18 AM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
simple GUI problem
By McChill in forum New To JavaReplies: 4Last Post: 04-03-2009, 03:13 AM -
Simple IO problem
By aamp in forum New To JavaReplies: 2Last Post: 12-01-2008, 02:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks