Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem getting a Variable Intialized.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem getting a Variable Intialized.

    So, every time I compile this, I get a single error on line 71 or the last part of the second If-Else statement. When I compile it, I get an error saying that my variable "bd" was not initialized.

    The line in question is
    else if (a == 3)
    {
    discount = bd;
    }

    I attached a zip with my java file in it, if that helps in solving the problem.

    Thanks in advance!




    import java.util.Scanner; //for the ability to get input from 
    						  //the keyboard 
    import java.text.DecimalFormat;
     
     
    public class WebsterAaronProject2
    {					  
     
    			public static void main(String[] args)
    	{
    			Scanner keyboard = new Scanner(System.in);
    			DecimalFormat df = new DecimalFormat("00.0");
    			double dp;
    			double sr = 5.00;
    			double at = 0;
    			double st = 0;
    			double bd;
    			double discount = 0;
    			double total = 0;
    			double beforeDiscount = 0;
    			int a;
     
     
     
     
    			System.out.println("******* WELCOME TO THE TICKET PRICE CALCULATOR ******");
    			System.out.println("                 created by Aaron Webster");
     
     
    			freezeScreen();
    			clearScreen();
     
    			System.out.print("How many adult tickets do you wish to purchase (ages 18-64): ");
    			at = keyboard.nextDouble();
     
     
    			System.out.print("How many student/senior tickets do you wish to purchase (0-17 or 65+): ");
    			st = keyboard.nextDouble();
     
    			clearScreen();
     
     
    			System.out.print("Please make a selection from the menu options below for additional savings.\n\n");
    			System.out.print("          Savings Menu \n ----------------------\n");
    			System.out.println("1. Free Shipping");
    			System.out.println("2. 10.0% Discount");
    			System.out.println("3. Apply whichever discount will save me the most money \n");
    			System.out.print("Enter your selection : ");
    			a = keyboard.nextInt();
     
    			dp = (at * 10 + st * 5) * .10; 
    			if (sr <= dp)
    			{
    			bd = dp;
    			}
    			else if (dp <= sr)
    			{
    			bd = sr;
    			}
     
    			if (a == 1)
    			{
    			discount = sr;
    			}
    			else if (a == 2)
    			{
    			discount = dp;
    			}
    			else if (a == 3)
    			{
    			discount = bd;
    			}
     
     
     
     
    			beforeDiscount = (at * 10) + (st * 5);
    			total = (beforeDiscount - discount) + sr;
     
     
     
     
    			clearScreen();
     
     
     
     
    			System.out.println("Purchase price $ " + beforeDiscount);
    			System.out.println("Discount applied $-" + discount);
    			System.out.println("Shipping $ " + sr);
    			System.out.println("--------------------------------------------");
    			System.out.print("Total Due $ " + total + "\n\n\n");
    			System.out.print("****** Thank you for using THE TICKET PRICE CALCULATOR *******");
     
    	} //end main
    	public static void clearScreen()
    	{
    		System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    	}//end clearScreen
     
    	public static void freezeScreen(String msg)
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print(msg);
    		keyboard.nextLine();
    	}//end freezeScreen
     
    	public static void freezeScreen()
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("	       -- Press Enter to Continue --");
    		keyboard.nextLine();
    	}
    }
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,046
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: Problem getting a Variable Intialized.

    variable "bd" was not initialized.
    The compiler doesn't consider what might happen inside of some if statements. It likes to see, for sure, that a variable has been assigned an initial value before it is used.
    Make the compiler happy by assigning bd a value when it is declared.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Problem getting a Variable Intialized.

    In the line discount = bd; bd holds no value. You need to initialize bd before assigning it to another variable.

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem getting a Variable Intialized.

    Ok I got it working, thanks man

Similar Threads

  1. variable double problem?
    By shen_punkz21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 09:00 AM
  2. Replies: 0
    Last Post: October 30th, 2012, 10:06 AM
  3. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  4. Replies: 5
    Last Post: November 16th, 2011, 10:22 AM
  5. variable might not be initialized problem. Can someone tell me whats wrong?
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 08:25 AM