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 3 of 3

Thread: final calculation after if statment

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default final calculation after if statment

    good evening peoples

    Once again I am on top of my head if anyone could point me in the right direction I would be very grateful.

    As you can see from snippet of code below I can chooce 1 of the following items (i have to pick 3 items so have the below done 3 times it works I am not to use loops yet)
    the problem is at the end of picking my products it should add up total. It dosent seem to do that without the ifs, if else it will. I have tryed putting the nested if I press 1 for apples then it just makes me take strawberries ..... thinking about this all day in work and its now 4.45 am

    System.out.print ("please select one of the following by pressing tne number on your keypad");
    System.out.println();
    System.out.println("1. Apples");
    System.out.println("2. Oranges");
    System.out.println("3. Straberries");
    System.out.println("4. Patato");
    System.out.println("5. Turnips");
    System.out.println("6. Carrots");

    fruit = in.nextInt();
    if (fruit <=1)
    {
    System.out.println ("Super choice");
    System.out.print("Enter quantity of apples required ");
    aQuantity = in.nextInt();
    appleCost = aPrice * aQuantity; //calculate final cost by adding tax
    String fs1 = String.format(" your apples will cost you: %.2f", appleCost);
    System.out.println(fs1);
    }

    else if (fruit <=2){

    System.out.print("Enter quantity of oranges required ");
    oQuantity = in.nextInt();
    orangesCost = oPrice * oQuantity; //calculate final cost by adding tax
    String fs2 = String.format(" your oranges will cost you: %.2f",orangesCost);
    System.out.println(fs2);
    }
    else if (fruit <=3){

    this is what im getting
    System.out.println(+appleCost + carrotCost+
    simple addition
    5 errors found:
    File: C:\Users\
    Error: variable appleCost might not have been initialized
    File: C:\Users\
    Error: variable carrotCost might not have been initialized

    How do i initalize appleCost ect if its inside an if and it needs user input????


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: final calculation after if statment

    I can only guess, since you haven't provided all of the code (in SSCCE form), but that error pretty much says it all. You're using a variable that might not have been initialized. For example, if you choose oranges, what will the value of apples be? Keep in mind that local variables do not have a default value.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: final calculation after if statment

    Pretty much what Kevin said. When you declare the variable "appleCost" im guessing your code says
    int appleCost;
    try putting
    int appleCost = 0;

Similar Threads

  1. Else-if statment failure
    By tyb97 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 17th, 2011, 08:52 PM
  2. [SOLVED] My string is not activating my if statment.
    By ZeroLRS in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 8th, 2011, 06:11 PM
  3. Switch Statment
    By 3XiLED in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2010, 05:11 PM
  4. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 11:37 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM