Exception in thread "main" java.util.InputMismatchException Using Double - java

import java.util.Scanner;
import java.text.DecimalFormat;
public class palomba2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
//allows input to be read
// VOLUME OF A PRISM
int prLngth, prWdth, prHght, prVol;
System.out.print("Enter the length of the prism in feet: ");
prLngth = keyboard.nextInt();
System.out.print("Enter the width of the prism in feet: ");
prWdth = keyboard.nextInt();
System.out.print("Enter the height of the prism in feet: ");
prHght = keyboard.nextInt();
prVol = prLngth * prWdth * prHght;
System.out.print("The volume of the prism is " + prVol);
System.out.print(" feet.");
System.out.println("");
System.out.println("");
// AREA/CIRCUMFERENCE OF A CIRCLE
DecimalFormat format = new DecimalFormat("0.##");
double radius, circ, area;
System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
radius = keyboard.nextInt();
circ = 2 * radius * 3.14159265358979;
area = radius * radius * 3.14159265358979;
System.out.print("The circumference of the circle is " + format.format(circ));
System.out.print("units.");
System.out.print("The area of the circle is " + format.format(area));
System.out.print("units.");
}
}
Everything in my code works up until I input the radius of the circle. It comes up with the error messages after I input a radius.
Enter the radius of a circle rounded to the nearest hundreth: 2.12
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:860)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextInt(Scanner.java:2161)
at java.base/java.util.Scanner.nextInt(Scanner.java:2115)
at palomba2.main(palomba2.java:52)

You type a double variable 2.12 but trying to read it as an int : input mismatch -> InputMismatchException
Also, the variable radius is of type double so you cannot give to it an int value (also you, you need :
radius = keyboard.nextDouble();
But still better to use radius = Double.parseDouble(keyboard.nextLine()) to consume the return char (same for int : int val = Integer.parseInt(keyboard.nextLine())

You just have to change the:
radius = keyboard.nextInt();
to:
radius = keyboard.nextDouble();
because the variable radius is declared as double
it should look like this:
double radius, circ, area;
System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
radius = keyboard.nextDouble();

Related

Running into errors while executing circle program in java

I am having trouble inputting the radius of a circle and angle of a circle (deg) while receiving the output with the values given plus the angle in radians, arc length and polar area all as floats. Here is what I have.
import java.util.Scanner;
public class MyCircles {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("1st: Please enter a radius (integer) :");
int radius01 = input.nextInt();
System.out.println("1st: Please enter angle (in degrees between 0 and 360) :");
int angledeg01 = input.nextInt();
input.close();
System.out.printf("%s%14s%15s%19s%24s\n", "Radius (inch)", "Angle (deg)", "Angle (rad)", "ArcLength (inch)", "Polar Area (sq. inch)");
System.out.printf("%13d%14d", radius01, angledeg01);
float anglerad01 = radius01 * (Math.PI / 180.0f);
Trying to achieve this:
Radius(inch) Angle(deg) Angle(rad) ArcLength(inch) Polar Area(sq.inch)
3 31 0.54 1.62 2.43
All in columns but I keep running into cannot convert into float error. Also cannot use the MathtoRadians functions. Must use hard code to show work. Suggestions?
anglerad01 should be double instead of float
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("1st: Please enter a radius (integer) :");
int radius01 = input.nextInt();
System.out.println("1st: Please enter angle (in degrees between 0 and 360) :");
int angledeg01 = input.nextInt();
input.close();
System.out.printf("%s%14s%15s%19s%24s\n", "Radius (inch)", "Angle (deg)", "Angle (rad)", "ArcLength (inch)", "Polar Area (sq. inch)");
System.out.printf("%13d%14d", radius01, angledeg01);
double anglerad01 = radius01 * (Math.PI / 180.0f);
}
}
All you have to do is convert Math.PI to a float since it is a double and not a float:
float anglerad01 = (radius01 * ((float)Math.PI / 180.0f));
The reason we need to do this conversion is because, while both floats and doubles are data types that represent decimals, they are not the same data type since doubles are more precise.
Math.PI gives you a constant in the form of a double, and so without casting to a float, you divide the double by a double, giving you a double, and then multiply it by an int to get a double. This leads you to be giving a float variable the value of a double.
However, once you cast Math.PI to a float, your final value will also be a float, making the variable legal.
First I read it wrong, I made the suggestion on the bases that Math.toRadians you are not able to use in your program due to some error.
So now its for what you have asked I hope.
1.data type double for anglerad01
2.As the expression will result a double its obvious as here we used Math.Pi.
3.float data type cannot take value of double data type.
4.So double anglerad01 should be used.
Also, for Radians = (Degrees × π)/180° . Degree is used to calculate radian not radius of circle.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("1st: Please enter a radius (integer) :");
int radius01 = input.nextInt();
System.out.println("1st: Please enter angle (in degrees between 0 and 360) :");
int angledeg01 = input.nextInt();
input.close();
double anglerad01 = radius01 * (Math.PI / 180.0f);
System.out.printf("%s%14s%15s%19s%24s\n", "Radius (inch)", "Angle (deg)", "Angle (rad)", "ArcLength (inch)", "Polar Area (sq. inch)");
System.out.printf("%13d%14d%18f", radius01, angledeg01, anglerad01 );
}

How do I change the type of number? Double to int

When I run this program, I need the part where it says example " cans needed 3.0" I need it to only say the integer.
public class PaintEstimator {
public static void main(String[] args) {
// Create the Scanner object scnr
Scanner scnr = new Scanner(System.in);
// Declare the identifiers
double wallHeight;
double wallWidth;
double wallAera;
double gallonPaint = 350;
int paintNeeded;
// Prompt user for and input wall's height and width; and then display them
System.out.println("Enter wall height (feet) :");
wallHeight = scnr.nextInt();
System.out.println("Enter wall width (feet) :");
wallWidth = scnr.nextInt();
System.out.println("Wall height is: " + wallHeight + " and Wall width is: " + wallWidth);
// Calculate and output wall area
wallAera = wallHeight * wallWidth;
System.out.println("Wall area: " + wallAera + "square feet");
// Calculate and output the amount of paint in gallons needed to paint the wall
System.out.println("Paint needed: " + (wallAera / gallonPaint) + " gallons");
// Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
System.out.println("Cans needed: " + Math.ceil(wallAera / gallonPaint) + " can ( s )");
// Calculate and output the cost of painting the wall.
double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
System.out.println("Cost to paint the wall: $" + costPaint);
}
}
You can round/ceil it up, as you are already doing, and then cast it to an int:
System.out.println("Cans needed: " + (int) Math.ceil(wallAera / gallonPaint) + " can ( s )");
There are 3 basic methods for change a double to an integer.
Where var-name is an integer and var-name1 is a double.
Method 1: Type-casting
Example:
int var-name = (int) var-name1;
Method 2: Rounding
Example:
int var-name = Math.round(var-name1);
Method 3: Double.intValue
Example:
int var-name = Double.intValue(var-name1);

Wall Painting Calculator. (two solutions - giving two different results)

A while ago I started to learn JAVA and everything was going well, until I got to this exercise:
CONDITIONS
Write a program that when complete will calculate the amount of paint needed to paint the walls and the ceiling of a room.
Your program should ask the length, width, and height of the room. Assume that the room has doors and windows the don't need painting. Also, the floor in the room is not painted.
Ask the user to enter the number of doors and number of windows in the room, and adjust the total square feet to be painted accordingly.
Assume that each door is 20 square feet and each window is 15 square feet.
Suppose the paint covers 350 square feet per gallon. enter code here
My chosen parameters
Width 3;
Length 10;
Height 2.0;
Doors 1;
Windows 2;
MY SOLUTION
int width = Integer.parseInt(JOptionPane.showInputDialog("Please, enter the width of a room"));
int length = Integer.parseInt(JOptionPane.showInputDialog("Please, enter the length of the room"));
double height = Double.parseDouble(JOptionPane.showInputDialog("Please, enter the height of the room"));
int doors = Integer.parseInt(JOptionPane.showInputDialog("If there are doors, please, enter the amount of them:"));
int windows = Integer.parseInt(JOptionPane.showInputDialog("If there are some windows, please, enter the amount of them:"));
double feet = 1.00/350.00;
double area = (width*length*height)-(windows*15+doors*20);
JOptionPane.showMessageDialog(null, "You need:"+feet*area+" gallons of paint.");
}
}
The output is 0.02857142
OTHER SOLUTIONby the author of the exercise
public static void main(String[] args) {
{
int length, width, numberOfDoors, numberOfWindows;
double height;
Scanner console = new Scanner(System.in);
System.out.print("Enter length: ");
length = console.nextInt();
System.out.print("Enter width: ");
width = console.nextInt();
System.out.print("Enter height: ");
height = console.nextDouble();
System.out.print("Enter number of doors: ");
numberOfDoors = console.nextInt();
System.out.print("Enter number of windows: ");
numberOfWindows = console.nextInt();
double totalSurfaceArea = 2 * (length * width + length
* height + width * height);
int areaOfFloor = length * width;
int areaOfDoors = 20 * numberOfDoors;
int areaOfWindows = 15 * numberOfWindows;
double totalPaintArea = totalSurfaceArea - areaOfFloor
- areaOfDoors - areaOfWindows;
double requiredPaint = totalPaintArea / 350;
System.out.println("Paint required "
+ requiredPaint + " gallons.");
}
}
}
The output is 0.09142857142857143;
So, two different solutions gave two different outputs.
Is there anyone who could help me to clarify, where I went wrong?

computing the volume of a cylinder using repeated addition?

im trying to make a code block to compute the volume of a cylinder but im unfamiliar with java so this is what i had so far. Is there anyway to re-purpose it so i can compute the volume?
package javaapplication231;
import java.util.Scanner;
public class JavaApplication231 {
public static void main(String[] args) {
int a,b,i,product;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of a and b: ");
a = keyboard.nextInt(); //Line 6
b = keyboard.nextInt();
i=0;
product=0;
while (i<a){
i=i+1;
product= product +b;
}
System.out.println("Product=" + product);
}
}
V =PIr^2h
where r is the radius and h is the height
user input: r and h
PI= 3.1416
To calculate the volume of Cylinder simply evaluate the expression, why do you need loop and do the addition. As you mentioned use the formula to calculate the
Volume=PI*radius2*height
int radius, height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of radius and Height: ");
radius = keyboard.nextInt(); // Line 6
height = keyboard.nextInt();
double volume = 3.14 * radius * radius * height;
System.out.println("Volume=" + volume);

Why won't my overloaded, static method recognize my height parameter?

So I have three static, overloaded methods that are used in my AreaClient class that are taking input from the user and passing what those inputs are as parameters to the methods below. For some reason though I can't seem to get the last area method to take in my hieght variable as a parameter. I keep getting an error saying "cannot find symbol". These are supposed to be overloaded methods, just what the assignment says. Sorry if this is real simple but I am pretty new to programming. Here is the code that I wrote.
import java.util.Scanner; // Needed for the Scanner class
public class AreaClient {
public static void main(String[] args) {
double circleRadius; //input for radius of circle
int width, length; //input for rectangle width and length
double cylinderRadius, height; //input for radius of a cylinder and hieght
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// gathering input for radius of circle
System.out.println("Enter radius of circle");
circleRadius = keyboard.nextDouble();
// input for width and length of rectangle
System.out.println("Enter width of rectangle");
width = keyboard.nextInt();
System.out.println("Enter length of rectangle");
length = keyboard.nextInt();
// input for radius and hieght of cylinder
System.out.println("Enter radius of cylinder");
cylinderRadius = keyboard.nextDouble();
System.out.println("Enter hieght of cylinder");
height = keyboard.nextDouble();
//returning area methods results and storing them in new variables
double circleArea = area(circleRadius);
int rectangleArea = area(width, length);
double cylinderArea = area(cylinderRadius, height);
//displaying results of methods
System.out.println("The area of your circle is: " + circleArea);
System.out.println("The area of your rectangle is: " + rectangleArea);
System.out.println("The area of your cylinger is: " + cylinderArea);
}
//overloaded methods that take different inputs
public static double area(double r)
{
return 3.14159265359 * Math.pow(r, 2);
}
public static int area(int w, int l)
{
return w * l;
}
//actual method that doesn't recognize h inside
public static double area(double r, double h)
{
return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
}
}
And the error msg I am getting
AreaClient.java:54: error: cannot find symbol
return 2*3.14159265359 * Math.pow(r,2) + h (2*3.14159265359*r);
^
symbol: method h(double)
location: class AreaClient
1 error
Thanks guys. Any help is much appreciated.
Notice in the error message:
symbol: method h(double)
Why it is looking for a method called h() which accepts a double? Because you're telling it to:
h (2*3.14159265359*r)
h isn't a method, it's just a value. You need to use an operator to connect it to that other value. I think you meant to do this:
h * (2*3.14159265359*r)
I think you mean: h * (2*3.14159265359*r). Without the operator, Java thinks you're trying to call a method named h(double)
return 2*3.14159265359 * Math.pow(r,2) + h * (2*3.14159265359*r);

Categories