# Logic error help

• November 9th, 2013, 11:49 AM
jganer
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:
Code java:

```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?
• November 9th, 2013, 12:34 PM
Norm
Re: Logic error help
Can you describe what the code is doing incorrectly?

[code=java]
[/code]
to get highlighting and preserve formatting.

Be sure the code is properly formatted. Nested statements should be indented.
• November 9th, 2013, 12:51 PM
jganer
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...
• November 9th, 2013, 12:53 PM
Norm
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?
• November 9th, 2013, 01:15 PM
GregBrannon
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.
• November 9th, 2013, 01:22 PM
jganer
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.
• November 9th, 2013, 01:29 PM
Norm
Re: Logic error help
Here's a sample of code with indentations to show nesting:
Code :

``` 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.