Java Troubleshooting Java Programming in Netbeans: Tips and Tricks for Beginners

  • Thread starter Thread starter furboll
  • Start date Start date
  • Tags Tags
    Java Programming
AI Thread Summary
The discussion revolves around a programming issue where a user is unable to get correct output from their paint estimator code in Java. The primary problem identified is that the user mistakenly reads the wall height variable twice instead of reading the wall width, resulting in a wall area calculation of zero. Additionally, there are suggestions to improve the code, such as removing unnecessary initializations of variables, correcting comments, and using the Math.ceil function to ensure the number of paint cans needed is rounded up correctly. The user expresses frustration with the textbook and learning resources, particularly regarding the use of NetBeans and outdated instructional videos. Overall, the conversation focuses on troubleshooting the code and enhancing the user’s understanding of Java programming concepts.
furboll
Messages
2
Reaction score
0
Hello everyone,
I am new to the forum. Programing was never an interest of mine but it’s an unfortunate requirement for classes I am taking I could really some help. I have tried fixing the code the best I can, but when I run this through Netbeans, no matter what numbers I put in for Wall height and Wall width, I still get an answer of 0. I have no idea what I am missing and I’ve read the (unhelpful) textbook over again. Is there anyone who sees what I am doing wrong and could explain to me ?

Code:
package paintestimator;

import java.util.Scanner;
import java.lang.Math;     
 
   public class paintEstimator {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      double wallHeight = 0.0;
      double wallWidth = 0.0;
      double wallArea = 0.0;
      double gallonsPaintNeeded = 0.0;
      int cansNeeded = 0;
      final double squareFeetPerGallons =350 ;
      final double gallonsPerCan = 1.0;

      System.out.println("Enter wall height (feet): ");
      wallHeight = scnr.nextDouble();

      // Prompt user to input wall's width
      System.out.println("Enter wall width (feet): ");
      wallHeight = scnr.nextDouble();

      // Calculate and output wall area
      wallArea = wallHeight * wallWidth;
      System.out.println("Wall area:  square feet "); // A space was needed in between square feet and "

      // Calculate and output the amount of paint in gallons needed to paint the wall
      gallonsPaintNeeded = wallArea / squareFeetPerGallons; // "/" A space should have beenmbetween wallArea and "/" and squareFeetPerGallons
      System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); // gallonspaintneeded needed to be corrected to gallonsPaintNeeded

      // Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
      cansNeeded = (int) (gallonsPaintNeeded / gallonsPerCan);   (needed to add "(int)" and () added around equation
      System.out.println("Cans needed: " + cansNeeded + " can(s)");  
 
     return; 
}
 
Technology news on Phys.org
Hey friend, 1. You might want to Try declaring the variables wallHeight, wallArea, gallonsPaintNeeded, and cansNeeded without the 0.0 like so:
(
double wallHeight;
double wallWidth;
double wallArea;
double gallonsPaintNeeded;
int cansNeeded;
)

2. There's an error in your last comment, it isn't a comment, so make it one.

3. unbalanced brackets

4. It also seems to me, you're entering for wallHeight twice instead of once for the height and once for the width. Tell me what you get :)
 
As indicated in the first response, the major problem is that you read wallHeight twice instead of a second read of wallWidth. Next problem: you convert a double to an int for number of cans needed. This "cast" to an int is done by just throwing away the decimal part (for a positive double); e.g. (int)0.57 is 0. So what your program needs is the next larger integer bigger than gallonsPaintNeeded/gallonsPerCan. In java.lang.Math is the function ceil which does exactly this. Here's your main method with the suggested tweaks:

Code:
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      double wallHeight = 0.0;
      double wallWidth = 0.0;
      double wallArea = 0.0;
      double gallonsPaintNeeded = 0.0;
      int cansNeeded = 0;
      final double squareFeetPerGallons = 350;
      final double gallonsPerCan = 1.0;

      System.out.println("Enter wall height (feet): ");
      wallHeight = scnr.nextDouble();

      // Prompt user to input wall's width
      System.out.println("Enter wall width (feet): ");
      wallWidth = scnr.nextDouble();

      // Calculate and output wall area
      wallArea = wallHeight * wallWidth;
      System.out.println("Wall area:  square feet " + wallArea); // A space was needed in between square feet and "

      // Calculate and output the amount of paint in gallons needed to paint the wall
      gallonsPaintNeeded = wallArea / squareFeetPerGallons; // "/" A space should have beenmbetween wallArea and "/" and squareFeetPerGallons
      System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); // gallonspaintneeded needed to be corrected to gallonsPaintNeeded

      // Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
      cansNeeded = (int) Math.ceil(gallonsPaintNeeded / gallonsPerCan);
      // (needed to add "(int)" and() added around equation 
      System.out.println("Cans needed: " + cansNeeded + " can(s)");

      return;
   }

Finally, you mentioned Netbeans. It will help you a lot if you learn to use the debugger; this shouldn't take too long.
 
Yohanna99 said:
Hey friend, 1. You might want to Try declaring the variables wallHeight, wallArea, gallonsPaintNeeded, and cansNeeded without the 0.0 like so:
(
double wallHeight;
double wallWidth;
double wallArea;
double gallonsPaintNeeded;
int cansNeeded;
)

2. There's an error in your last comment, it isn't a comment, so make it one.

3. unbalanced brackets

4. It also seems to me, you're entering for wallHeight twice instead of once for the height and once for the width. Tell me what you get :)
Thanks so much. You were right about me looking for wallHeight twice, as well as the other issues you pointed out. Thanks!

- - - Updated - - -

johng said:
As indicated in the first response, the major problem is that you read wallHeight twice instead of a second read of wallWidth. Next problem: you convert a double to an int for number of cans needed. This "cast" to an int is done by just throwing away the decimal part (for a positive double); e.g. (int)0.57 is 0. So what your program needs is the next larger integer bigger than gallonsPaintNeeded/gallonsPerCan. In java.lang.Math is the function ceil which does exactly this. Here's your main method with the suggested tweaks:

Code:
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      double wallHeight = 0.0;
      double wallWidth = 0.0;
      double wallArea = 0.0;
      double gallonsPaintNeeded = 0.0;
      int cansNeeded = 0;
      final double squareFeetPerGallons = 350;
      final double gallonsPerCan = 1.0;

      System.out.println("Enter wall height (feet): ");
      wallHeight = scnr.nextDouble();

      // Prompt user to input wall's width
      System.out.println("Enter wall width (feet): ");
      wallWidth = scnr.nextDouble();

      // Calculate and output wall area
      wallArea = wallHeight * wallWidth;
      System.out.println("Wall area:  square feet " + wallArea); // A space was needed in between square feet and "

      // Calculate and output the amount of paint in gallons needed to paint the wall
      gallonsPaintNeeded = wallArea / squareFeetPerGallons; // "/" A space should have beenmbetween wallArea and "/" and squareFeetPerGallons
      System.out.println("Paint needed: " + gallonsPaintNeeded + " gallons"); // gallonspaintneeded needed to be corrected to gallonsPaintNeeded

      // Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
      cansNeeded = (int) Math.ceil(gallonsPaintNeeded / gallonsPerCan);
      // (needed to add "(int)" and() added around equation 
      System.out.println("Cans needed: " + cansNeeded + " can(s)");

      return;
   }

Finally, you mentioned Netbeans. It will help you a lot if you learn to use the debugger; this shouldn't take too long.

And thank you for pointing out I didn't have the Math.ceil in my equation.

As far as learning Netbeans goes, I am trying my best with it. Unfortunately the video the school sent out was older and they were using JGrasp, but we were told "they work the same". So basically instead of doing an updated video they told us to apply the same functions in Netbeans. And to make matters worse the audio and video are horrible int he video they sent us.

Thank you.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
0
Views
318
Replies
19
Views
2K
Replies
1
Views
6K
Replies
0
Views
797
Replies
1
Views
2K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
3
Views
3K
Back
Top