Results 1 to 10 of 10
  1. #1
    M1NO is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Exclamation Help with Simple If-Else problem!

    Getting this Error when I try to compile.
    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
    Trying to Compile this:

    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;
    		
    		
    	}
    }
    Anyone know what I'm doing wrong? =/

  2. #2
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    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.

  3. #3
    M1NO is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Vase View Post
    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.
    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;

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by M1NO View Post
    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;
    You declared them in "public static void main", which is very much a method.

    You can solve it by declaring them like "double singleTree = 0;" or declaring them outside any methods.

  5. #5
    M1NO is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Solarsonic View Post
    You declared them in "public static void main", which is very much a method.

    You can solve it by declaring them like "double singleTree = 0;" or declaring them outside any methods.
    But I need the singleTree to equal the specific amount according to the different circumstances when a user inputs a number..

  6. #6
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    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.");
    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).

    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.

  7. #7
    M1NO is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Vase View Post
    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.");
    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).

    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.
    Ah, okay so where in my whole script would be a good place to Initialize singleTree and deliveryCost to 0?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by M1NO View Post
    But I need the singleTree to equal the specific amount according to the different circumstances when a user inputs a number..
    Assigning a default value (zero) is only the initial value, hence the term initialise. You can change the value as many times as you like afterwards, unless you make it final.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Ah, okay so where in my whole script would be a good place to Initialize singleTree and deliveryCost to 0?
    Where you declare them.

  10. #10
    M1NO is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Sweet!
    I made my declaration of my variables to:

    int forDelivery;
    int numTrees;
    int treeHeight;
    double singleTree = 0;
    double treeCost;
    double deliveryCost = 0;
    double totalCharges;

    and I'm no longer getting errors! thanks guys

Similar Threads

  1. Very simple problem
    By Andyj in forum Java Certification
    Replies: 3
    Last Post: 09-17-2010, 06:49 AM
  2. Need help with simple problem
    By newb_tewb in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 02:18 AM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. simple GUI problem
    By McChill in forum New To Java
    Replies: 4
    Last Post: 04-03-2009, 03:13 AM
  5. Simple IO problem
    By aamp in forum New To Java
    Replies: 2
    Last Post: 12-01-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •