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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
0
Views
412
Replies
19
Views
3K
Replies
1
Views
6K
Replies
0
Views
887
Replies
1
Views
2K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
3
Views
3K
Back
Top