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: simple addition inside { of after the if

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

    Default simple addition inside { of after the if

    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 noe 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){
    Last edited by helloworld922; October 23rd, 2011 at 12:58 AM.


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

    Default Re: simple addition inside { of after the if

    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????

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: simple addition inside { of after the if

    The compiler does not know that you have given it a value, for all it know's your trying to do
    int carrots;
    if(1 == 2)
    {
      carrots = 2;
    }
    System.out.println(carrots);
    , what is it suppose to print then?

    To initialize it, give it some default value, -1 is popular
    int appleCost = -1;
    int carrotCost = -1
    //if statement, get user input, set the variable

Similar Threads

  1. [SOLVED] Addition Quiz
    By javaneedhelp in forum Loops & Control Statements
    Replies: 5
    Last Post: October 9th, 2011, 01:55 AM
  2. Addition
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 14th, 2011, 11:49 PM
  3. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 10:52 AM
  4. text value addition problem
    By kundan_101 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: January 8th, 2011, 11:46 AM