Troubleshooting Java Programming in Netbeans: Tips and Tricks for Beginners

In summary, the conversation is about a person seeking help with a programming project on a forum. They are having trouble with their code and have received some suggestions for improving it, including declaring variables without a value, using brackets correctly, and using the Math.ceil function for rounding up to the nearest integer. The code in question involves calculating the area of a wall and determining the amount of paint needed to cover it.
  • #1
furboll
2
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
  • #2
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 :)
 
  • #3
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.
 
  • #4
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.
 

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

What is Java programming?

Java programming is a popular object-oriented programming language that is widely used for developing web and mobile applications, as well as desktop software. It was created by James Gosling at Sun Microsystems in 1995.

What are the basic concepts of Java programming?

The basic concepts of Java programming include classes, objects, inheritance, encapsulation, polymorphism, data types, variables, control structures, and exception handling. Understanding these concepts is crucial for writing efficient and functional Java code.

What are some common challenges when learning Java programming?

Some common challenges when learning Java programming include understanding the syntax and structure of the language, grasping the concept of object-oriented programming, and troubleshooting errors. It is also important to practice regularly and familiarize yourself with libraries and frameworks commonly used in Java development.

What are some helpful resources for learning Java programming?

There are many resources available for learning Java programming, including online tutorials, textbooks, and video courses. Some popular resources include Oracle's official Java tutorials, Codeacademy, and Udemy. Additionally, joining online communities and forums can also be helpful for beginners to ask questions and get support from more experienced Java programmers.

What are some best practices for writing efficient Java code?

Some best practices for writing efficient Java code include using meaningful and descriptive variable names, following proper naming conventions, avoiding unnecessary nested loops, and using appropriate data structures and algorithms. It is also important to regularly test and debug your code to catch any errors or performance issues.

Similar threads

Replies
19
Views
2K
Replies
1
Views
6K
Replies
1
Views
2K
Replies
3
Views
1K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
4
Views
3K
Back
Top