Trouble with the output - java

The following code creates an error for me after running it, it projects the first line "Please enter length of a rectangle." correctly, but then the next line after I input a number is "Enter the length of the rectangle: Please enter width of a rectangle." Then after I input the second time it creates an error and crashes my code. To clarify I don't mean to have those next to each other like that. Any help would be appreciated. Thanks!
import java.util.Scanner;
public class AreaRectangle {
public static void main(String[] args) {
double length, // The rectangle's length
width, // The rectangle's width
area; // The rectangle's area
// Get the rectangle's length from the user.
length = getLength();
System.out.print("Enter the length of " +
"the rectangle: ");
// Get the rectangle's width from the user.
width = getWidth();
System.out.print("Enter the width of " +
"the rectangle: ");
// Get the rectangle's area.
area = getArea(length, width);
System.out.print("The area of the " +
"rectangle is: ");
// Display the rectangle data.
displayData(length, width, area);
System.out.print("Enter the length of " +
"the rectangle: ");
}
public static double getLength() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter length of a rectangle.");
double length = keyboard.nextDouble();
System.out.println("The length of the rectangle is " + length);
return length;
}
public static double getWidth() {
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Please enter width of a rectangle.");
width = keyboard.nextDouble();
System.out.println("The width of the rectangle is " + width);
return width;
}
public static double getArea(double length, double width) {
double area = length * width;
System.out.println("The area of the rectangle is " + area);
return area;
}
public static double displayData(double length, double width, double
area) {
System.out.println("The length of the rectangle is: \t" + length);
System.out.println("The width of the rectangle is: \t" + width);
System.out.println("The area of the rectangle is: \t" + area);
}
}

Remove all the prompts from main(). The proper prompts are inside the other methods.
In each method that you open a Scanner object,
don't forget to close it just before the return statement: keyboard.close();.

You forgot to return value from the getLength function.
Just add a return statement on it and it will work.
you have declared:
public static double displayData(....)
so you MUST return a double value or change your function declaration to
public static void displayData
You have also another problem with your main function: you can delete all the println on the main scope. You can also remove the println inside getArea function. your code will be like:
import java.util.Scanner;
public class AreaRectangle {
public static void main(String[] args) {
double length, // The rectangle's length
width, // The rectangle's width
area; // The rectangle's area
// Get the rectangle's length from the user.
length = getLength();
// Get the rectangle's width from the user.
width = getWidth();
// Get the rectangle's area.
area = getArea(length, width);
// Display the rectangle data.
displayData(length, width, area);
}
public static double getLength() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter length of a rectangle.");
double length = keyboard.nextDouble();
System.out.println("The length of the rectangle is " + length);
return length;
}
public static double getWidth() {
Scanner keyboard = new Scanner(System.in);
double width;
System.out.println("Please enter width of a rectangle.");
width = keyboard.nextDouble();
System.out.println("The width of the rectangle is " + width);
return width;
}
public static double getArea(double length, double width) {
double area = length * width;
return area;
}
public static void displayData(double length, double width, double
area) {
System.out.println("The length of the rectangle is: \t" + length);
System.out.println("The width of the rectangle is: \t" + width);
System.out.println("The area of the rectangle is: \t" + area);
}
}
and the output of your console will be ok for your purpose.

Here is the code for you. Take a look how it is implemented and try adding some new features like parameter or diagonal of the rectangle. It isn't that hard.
public class Main {
private static double lengthOfRectangle;
private static double widthOfRectangle;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Input the length of the rectangle: \n");
lengthOfRectangle = scanner.nextDouble();
System.out.println("Input the length of the rectangle: \n");
widthOfRectangle = scanner.nextDouble();
System.out.printf("Length: %s - Width: %s - Area of your rectangle is %s", lengthOfRectangle,widthOfRectangle,lengthOfRectangle*widthOfRectangle);
}
}

Related

User input values for a Rectangle

I have a class where a user inputs the x and y coordinates for the top left corner of a perfect rectangle, as well as the length and width. The RectangleViewer constructor sets the rectangle as null and then sets all the variables to 0.
Here is what I have so far:
public class RectangleViewer {
private Rectangle rectangle;
private Scanner input = new Scanner(System.in);
public RectangleViewer() {
this.rectangle = null;
this.rectangle = new Rectangle(new Point2D.Double(0.0, 0.0), 0.0, 0.0);
}
public void inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
}
public void initializeRectangle() {
this.rectangle = new Rectangle(new Point2D.Double(inputRectangle());
}
}
Here is the code for the Rectangle class itself:
public class Rectangle {
private double width;
private double length;
private Point2D.Double topLeftPoint;
public Rectangle(Point2D.Double topLeftPoint, double width, double length) {
this.width = width;
this.length = length;
this.topLeftPoint = topLeftPoint;
}
How would I set it up so that the values that are put in inputRectangleParameters are assigned to a rectangle that will be created in initializeRectangle?
The method inputRectangleParameters reads values from the command line. If you wish to create the rectangle in the initializeRectangle method, the read values need to be passed to it.
This could be done by e.g. returning the values in an object.
As the Rectangle class contains according attributes for the values, I'd recommend to create a rectangle object within the input method and returning it to initialize.
public Rectangle inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
return new Rectangle(new Point2D.Double(xCoordinate,yCoordinate),inputWidth,inputLength);
}
public void initializeRectangle() {
this.rectangle = inputRectangleParameters();
}
A less clean version would be to return e.g. an array of the values with specific positions of values.
public double[] inputRectangleParameters() {
System.out.print("Please enter the x coordinate of the top left point of the rectangle: ");
double xCoordinate;
xCoordinate = input.nextDouble();
System.out.print("Please enter the y coordinate of the top left point of the rectangle: ");
double yCoordinate;
yCoordinate = input.nextDouble();
System.out.print("Please enter the length of the rectangle: ");
double inputLength;
inputLength = input.nextDouble();
System.out.print("Please enter the width of the rectangle: ");
double inputWidth;
inputWidth = input.nextDouble();
return new double[]{xCoordinate, yCoordinate, inputWidth, inputLength};
}
public void initializeRectangle() {
final double[] rectangleParameters = inputRectangleParameters();
this.rectangle = new Rectangle(new Point2D.Double(rectangleParameters[0], rectangleParameters[1]), rectangleParameters[2], rectangleParameters[3]);
}

Code runs but its not giving me the correct output

I'm fairly new to coding. I'm not sure how to move the input values across the methods. I tried different ways but it always gave me an error. Also I'm curious as to how you would return multiple values. Like lets say instead of having getLength() and getWidth() there would be one called getInput() were both length and width would be returned.
import java.util.Scanner; // Needed for the Scanner class
/**
This program calculates the area of a rectangle.
*/
public class Rectangle
{
//main method
public static void main(String[] args)
{
float length = 0;
float width = 0;
float area = 0;
getLength();
getWidth();
calcArea(length, width);
displayData(area);
}
//method 2
public static float getLength()
{
// Create a Scanner object to read from the keyboard.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Length: ");
float length = keyboard.nextFloat();
return length;
}
public static float getWidth()
{
// Create a Scanner object to read from the keyboard.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Width: ");
float width = keyboard.nextFloat();
return width;
}
public static float calcArea(float length, float width)
{
float area = length*width;
System.out.print("\nxxxx "+area);
return area;
}
public static void displayData(float area)
{
System.out.print("\nThe area of the rectangle is "+area);
}
}
Your problem comes from the fact that non-void methods will return values. If you don't assign those values to any variables, Java won't do anything with them.
float length = 0;
float width = 0;
float area = 0;
// Values are retrieved, but must be assigned to variables
length = getLength();
width = getWidth();
area = calcArea(length, width);
I urge to you try something a little more intuitive. I understand that you're new to coding, but try messing around with this and see where it gets you. OOP is a core component of Java and it's generally good practice to use it whenever possible.
Rectangle.java:
public class Rectangle
{
private float length;
private float width;
public Rectangle(float length, float width)
{
this.length = length;
this.width = width;
}
public float getArea()
{
return length * width;
}
public float getLength()
{
return length;
}
public float getWidth()
{
return width;
}
}
Main class:
public static void main(String[] args)
{
// Only open one stream at a time
// Opening multiple without closing the previous may cause some problems in the future
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Length: ");
float length = keyboard.nextFloat();
System.out.print("Enter Width: ");
float width = keyboard.nextFloat();
// Remember to close the stream when you are finished using it
keyboard.close();
// Create a new rectangle with the length and width that was given by the user
Rectangle rect = new Rectangle(length, width);
// Round the area to the nearest tenth of a decimal place and display results
System.out.printf("The area of the rectangle is %.1f", rect.getArea());
}
This should help:
length = getLength();
width = getWidth();
area = calcArea(length, width);
displayData(area);

Use the results from one method to calculate the price in another method

I'm a beginner.
I'm having trouble figuring out what exactly I'm doing wrong.
I have managed to get the first method working fine(calculateArea) but I don't understand why the second method isn't working(can't find symbol of area)
import java.util.Scanner;
public class PaintCalculator
{
public static void main(String[] args)
{
double length;
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length >> ");
length = keyboard.nextDouble();
System.out.print("Enter the width >> ");
width = keyboard.nextDouble();
keyboard.nextLine();
calculateArea(length, width);
calculatePrice(area);
}
public static double calculateArea (double length, double width)
{
double area;
area = length * width;
System.out.println(area);
return area;
}
public static void calculatePrice(double area)
{
double gallons = area * 350;
double price = gallons * 32;
System.out.println(price);
}
}
Sorry if this is a noob question
You have to save the returned value from calculateArea (which is area) to double variable and the pass that to calculatePrice
public static void main(String[] args)
{
double length;
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length >> ");
length = keyboard.nextDouble();
System.out.print("Enter the width >> ");
width = keyboard.nextDouble();
keyboard.nextLine();
double area = calculateArea(length, width);
calculatePrice(area);
}
This is a classic example of how java handles data within methods. The area variable is within your calculateArea method and you are trying to use it via main and feed it into calculate price. instead assign the area to a variable and pass it in
{
public static void main(String[] args)
{
double length;
double width;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length >> ");
length = keyboard.nextDouble();
System.out.print("Enter the width >> ");
width = keyboard.nextDouble();
keyboard.nextLine();
//calculateArea(length, width);
int area = calculateArea(length, width);
calculatePrice(area);
//or just directly place it in like this
calculatePrice(calcuateArea(length, width));
}
public static double calculateArea (double length, double width)
{
double area;
area = length * width;
System.out.println(area);
return area;
}
public static void calculatePrice(double area)
{
double gallons = area * 350;
double price = gallons * 32;
System.out.println(price);
}
}

Why am I getting a "void could not be converted to int" error with JOptionPane.showInputDialog?

I have this assignment for school where I have to use JOptionPane to calculate the area of different shapes, but when I use JOptionPane.showMessageDialog, it turns red and the error code says that void cannot be converted to int. Can someone help me figure out how to use JOptionPane.showMessageDialog?
public static void areaTriangle(int base, int height)
{
System.out.println(0.5 * base * height);
}
public static void areaCircle(int radius, double area)
{
area = Math.PI * (radius * radius);
System.out.println(area);
}
public static void areaRectangle(int length, int width)
{
System.out.println(length * width);
}
public static void calcArea()
{
System.out.println("What type of area do you want to calculate?");
inputStr = JOptionPane.showInputDialog("Type 1 for triangle, 2 for circle, 3 for rectangle, and 0 for none, then press ENTER.");
int type = Integer.parseInt(inputStr);
if (type == 0)
{
return;
}
else if (type == 1)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the base, then press ENTER.");
int base = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Enter the measurement of the height, then press ENTER.");
int height = Integer.parseInt(inputStr);
int areaT = areaTriangle (base, height);
JOptionPane.showMessageDialog(null, "The area of the triangle is: " + areaT);
}
else if (type == 2)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the radius, then press ENTER.");
int radius = Integer.parseInt(inputStr);
double area = Math.PI * (radius * radius);
int areaC = areaCircle (radius, area);
JOptionPane.showMessageDialog(null, "The area of the cicle is: " + areaC);
}
else if (type == 3)
{
inputStr = JOptionPane.showInputDialog("Enter the measurement of the length, then press ENTER.");
int length = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Enter the measurement of the width, then press ENTER.");
int width = Integer.parseInt(inputStr);
int areaR = areaRectangle(length, width);
JOptionPane.showMessageDialog(null, "The area of the rectangle is: " + areaR);
}
}
public static void main(String[] args)
{
calcArea();
}
Your problem has nothing to do with the JOptionPane and everything to do with your area* methods
First you declare all your methods as void...
public static void areaTriangle(int base, int height) {
System.out.println(0.5 * base * height);
}
public static void areaCircle(int radius, double area) {
area = Math.PI * (radius * radius);
System.out.println(area);
}
public static void areaRectangle(int length, int width) {
System.out.println(length * width);
}
But then you try and assign a return result to a variable when you call them...
int areaT = areaTriangle(base, height);
//...
int areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);
Which simply makes no sense.
Based on what you seem to be trying to do, you need to change your area* methods to return a value of some kind...
public static double areaTriangle(int base, int height) {
return 0.5 * base * height;
}
public static double areaCircle(int radius, double area) {
return Math.PI * (radius * radius);
}
public static int areaRectangle(int length, int width) {
return length * width;
}
Then you can assign the return values from the methods to some variables...
double areaT = areaTriangle(base, height);
//...
double areaC = areaCircle(radius, area);
//...
int areaR = areaRectangle(length, width);

My program will not display any output

My code compiles, but the output won't show. I think my methods are all correct, but I have no idea why my output will not display. (NB: beginner in Java)
import java.util.Scanner;
import java.lang.Math;
public class Lab8 {
public static void main(String[] args) {
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = in.nextDouble();
System.out.print("Enter height: ");
height = in.nextDouble();
}
public static double Calculations(double radius, double height) {
double surfaceArea = (2 * Math.PI * radius * radius) + (2 * Math.PI * radius * height);
return surfaceArea;
}
public static double calculations(double radius, double height) {
double volume = Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume) {
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
You need to call your method inside main to print the value.
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
Fixed Code:
import java.util.Scanner;
import java.lang.Math;
public class Lab8
{
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
System.out.println("surfacearea " + Calculations(radius,height));
System.out.println("volume " +calculations(radius,height));
System.out.println("output " + output(radius,height));
}
public static double Calculations(double radius, double height)
{
double surfaceArea= (2 * Math.PI * radius * radius) + (2 * Math.PI *radius* height);
return surfaceArea;
}
public static double calculations(double radius,double height)
{
double volume= Math.PI * radius * radius * height;
return volume;
}
public static void output(double surfaceArea, double volume)
{
System.out.println("Surface Area of Cylinder: " + surfaceArea);
System.out.println("Voulme of Cylinder: " + volume);
}
}
Try calling one of your Calculations method in your main method, post taking the input such as:
System.out.println(Lab8.Calculations(radius,height));
In main you miss the calculate and output method call
public static void main(String[] args)
{
double radius;
double height;
Scanner in = new Scanner(System.in);
System.out.print("Enter radius: ");
radius= in.nextDouble();
System.out.print("Enter height: ");
height= in.nextDouble();
// call this
System.out.println("area " + output(Calculations(radius,height)));
}
You don't actually call your methods.
In main, after you get the radius and height, call Calculations and calculations respectively, with the appropriate arguments.
Here's an example:
surfaceArea = Calculations(radius, height);
volume = calculations(radius, height);
You didn't call any of your helper functions within main(...). Try adding a call to output or calculations within your main function.
In particular, something like
output(Calculations(radius, height), calculations(radius, height));
will probably suffice.
As you learn Java, it may be helpful to begin applying descriptive names to your functions, such as CylinderSurfaceArea to help keep confusion to a minimum both for you and for anyone who might read code you have written.

Categories