Question

In: Computer Science

HI. I have been trying to run my code but I keep getting the following error....

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);

}

}

}

}

}

}

Solutions

Expert Solution

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


Related Solutions

My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
Need C++ code to be able to run, keep getting a constant value error #include #include...
Need C++ code to be able to run, keep getting a constant value error #include #include #include #include #include #include using namespace std; using namespace std::chrono; int c; void insertionSort(int* arr, int n) { for (int i = 1;i < n;i++) { int v = arr[i]; int j; for (j = i - 1;j > -1;j--) { c++; if (arr[j] > v) { arr[j + 1] = arr[j]; } else { break; } } arr[j + 1] = v; }...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
Why do I keep getting the error "More columns than column names" when trying to import...
Why do I keep getting the error "More columns than column names" when trying to import the following data into R Studio?: PRICE   YEARSOLD   MILES   COLOR   TITLESTATUS   TRANSMISSION   4500   11   170000   SILVER   CLEAN   MANUAL           34590   1   2000   SILVER   CLEAN   AUTOMATIC   4500   14   203000   SILVER   CLEAN   AUTOMATIC   11990   6   53337   GRAY   CLEAN   AUTOMATIC   10490   8   36543   RED   CLEAN   AUTOMATIC   2800   19   208000   SILVER   CLEAN   MANUAL       1200   19   244000   SILVER   CLEAN   AUTOMATIC   2500   19   208000   BROWN   CLEAN   AUTOMATIC   2000  ...
Hi I have a java code for my assignment and I have problem with one of...
Hi I have a java code for my assignment and I have problem with one of my methods(slice).the error is Exception in thread "main" java.lang.StackOverflowError Slice method spec: Method Name: slice Return Type: Tuple (with proper generics) Method Parameters: Start (inclusive) and stop (exclusive) indexes. Both of these parameters are "Integer" types (not "int" types). Like "get" above, indexes may be positive or negative. Indexes may be null. Description: Positive indexes work in the normal way Negative indexes are described...
hi! I have this code. when I run it, it works but in the print(the number...
hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works #include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int n,i; printf("Give me the size of the table : \n"); scanf("%d", &n); int A[n],x,y; for (i=0;i<n;i++) A[i]=rand() % n; for (i=0;i<n;i++) printf("%d\n", A[i]); srand(time(NULL)); y=rand()% n ; printf("The number we are searching...
I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT