In: Computer Science
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method.
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at project2.areacalculation.main(areacalculation.java:26)
My code is below
package project2;
import java.util.Scanner;
public class areacalculation {
private static final int S = 0;
private static final int R = 0;
private static final int C = 0;
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
System.out.println("Please enter a letter (S,R,C): ");
System.out.println("\tS -- Square");
System.out.println("\tR -- Rectangle");
System.out.println("\tC -- Circle");
//Wait for user input
int option = scan.nextInt();
if (option==S); {
//Calculate the area of a Square
System.out.println("Please enter the length of the square");
double l = scan.nextDouble();
//Area calculation
double area = l*l;
System.out.println("Area: " + area);
}
{
if(option==R); {
//Calculate the area of a Rectangle
System.out.println("Please enter the lenghth of the rectangle");
double l = scan.nextDouble();
System.out.println("Please enter the width of the rectangle");
double w = scan.nextDouble();
//Area calculation
double area = l * w;
System.out.println("Area: " + area);
}
{if(option==C); {
//Calculate the area of a Circle
System.out.println("Please enter the radius of the circle");
double r = scan.nextDouble();
double area = Math.PI * r * r;
System.out.println("Area : " + area);
}
}
}
}
}
}
import java.util.Scanner;
public class areacalculation {
private static final char S = 'S';
private static final char R = 'R';
private static final char C = 'C';
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
System.out.println("Please enter a letter (S,R,C): ");
System.out.println("\tS -- Square");
System.out.println("\tR -- Rectangle");
System.out.println("\tC -- Circle");
// Wait for user input
char option = scan.next().charAt(0);
if (option == S) {
// Calculate the area of a Square
System.out.println("Please enter the length of the square");
double l = scan.nextDouble();
// Area calculation
double area = l * l;
System.out.println("Area: " + area);
}
else if (option == R) {
// Calculate the area of a Rectangle
System.out.println("Please enter the lenghth of the rectangle");
double l = scan.nextDouble();
System.out.println("Please enter the width of the rectangle");
double w = scan.nextDouble();
// Area calculation
double area = l * w;
System.out.println("Area: " + area);
}
else if (option == C) {
// Calculate the area of a Circle
System.out.println("Please enter the radius of the circle");
double r = scan.nextDouble();
double area = Math.PI * r * r;
System.out.println("Area : " + area);
}
}
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME