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

Thread: Logic error help

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Logic error help

    Hello, I need some help in finding some logic errors in my code. I still new to programming in Java, so the more help the better!
    Anyways here is the questio I'm trying to solve:
    During each summer John and Jessica grow vegetables in their back yard and
    buy seeds and fertilizer from a local nursery. The nursery carries different
    types of vegetable fertilizers in various bag sizes. When buying a particular
    fertilizer, they want to know the price of the fertilizer per pound and the cost
    of fertilizing per square foot. The following program prompts the user to
    enter the size of the fertilizer bag, in pounds, the cost of the bag, and the
    area, in square feet, that can be covered by the bag. The program should
    output the desired result. However, the program contains logic errors. Find
    and correct the logic errors so that the program works properly.

    And here is my code:
    import java.util.*;
    public class Ch3_PrExercise4
    {
    static Scanner console = new Scanner(System.in);
    public static void main(String[] args)
    {
    double cost;
    double area;
    double bagSize;
    System.out.print("Enter the amount of fertilizer, "
    + "in pounds, in one bag: ");
    bagSize = console.nextDouble();
    System.out.println();
    System.out.print("Enter the cost of the " + bagSize
    + " pound fertilizer bag: ");
    cost = console.nextDouble();
    System.out.println();
    System.out.print("Enter the area, in square feet, that "
    + "can be fertilized by one bag: ");
    area = console.nextDouble();
    System.out.println();
    System.out.printf("The cost of the fertilizer per pound is: "
    + "$%.2f%n", bagSize / cost);
    System.out.printf("The cost of fertilizing per square "
    + "foot is: $%.4f%n", area / cost);
    }
    }



    Whats wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logic error help

    Can you describe what the code is doing incorrectly?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Be sure the code is properly formatted. Nested statements should be indented.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logic error help

    I edited my post and to answer your other question I don't know whats wrong with it. I'm using notepad++ and it's not telling me. I had somebody review the code and they said there were logic errors I needed to fix within my code. I'm still new to this and I'm so confused right now...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logic error help

    Please edit the code and format it properly. Nested statements should be indented.

    What happens when the code is executed? Does the output look correct for the data that was entered as input?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Logic error help

    Asking us to find the errors for you sounds like we'd be doing the assignment exactly as it was given to you. Why don't you tell us where you think the errors are and we'll let you know if we agree and if you've found them all.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Logic error help

    What are nested statements and what part needs to be edited still? Also, I cant run the program since I only have notepad++ right now and had somebody else review it. I just started last week, so I just want to say sorry in advance for my greenhorn knowledge at the moment.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logic error help

    Here's a sample of code with indentations to show nesting:
          int size = 6;
          for(int lineNbr=0; lineNbr < size; lineNbr++) {
             // print size - lineNbr spaces
             for(int nS = 0; nS < (size - lineNbr); nS++) {
                System.out.print(" ");
             } 
             // now print lineNbr *s                 0=1, 1=3, 2=5, 3=7 >>>> lineNbr*2 + 1
             for(int nA = 0; nA < (lineNbr*2)+1; nA++){
                System.out.print("*");
             }
             System.out.println();  // move to new line
          } // end for(lineNbr)

    You need to download the JDK so you can compile and execute the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. logic error in for loops
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2013, 09:35 AM
  2. Vector of array [logic error]
    By Rexshine in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 3rd, 2013, 04:02 PM
  3. Not sure what the Logic Error is? [help]
    By Mitsuwa in forum Object Oriented Programming
    Replies: 2
    Last Post: January 27th, 2013, 11:55 PM
  4. Can you help me find my logic error
    By michael305rodri in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 5th, 2012, 01:51 AM
  5. Lotto Problem logic error
    By ippo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 10th, 2012, 10:18 AM