Troubleshooting Java Programming in Netbeans: Tips and Tricks for Beginners

  • Context: Java 
  • Thread starter Thread starter furboll
  • Start date Start date
  • Tags Tags
    Java Programming
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Java programming issue in Netbeans, specifically related to a paint estimator application. Participants are addressing problems with variable assignments, calculations, and the overall logic of the code. The focus is on debugging and improving the code structure for beginners.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the code incorrectly reads the wall height variable twice instead of reading the wall width, leading to a consistent output of zero.
  • Another participant suggests declaring variables without initial values, indicating that this might help clarify their usage.
  • There is mention of an error in the comments within the code, which some participants argue should be properly formatted as comments.
  • A participant points out that the conversion from double to int for calculating the number of cans needed discards the decimal part, which could lead to incorrect results.
  • Some participants propose using the Math.ceil function to ensure the number of cans needed is rounded up to the next whole number.
  • There is a discussion about the challenges of learning to use Netbeans, with one participant expressing frustration over outdated instructional materials provided by their school.

Areas of Agreement / Disagreement

Participants generally agree on the main issues with the code, particularly regarding the reading of variables and the need for proper rounding. However, there are variations in the suggestions for corrections, and no consensus is reached on the best approach to resolve all issues.

Contextual Notes

Some limitations in the discussion include unresolved mathematical steps related to the calculation of cans needed and the potential impact of variable initialization on program behavior. The discussion also reflects a dependency on the specific context of using Netbeans and the challenges faced by beginners.

Who May Find This Useful

This discussion may be useful for beginners in programming, particularly those learning Java and using Netbeans, as well as educators looking for insights into common troubleshooting issues faced by students.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
957
Replies
1
Views
8K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K