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)...
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  ...
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);...
Keep getting error where the code cannot read the text file and create an arraylist of...
Keep getting error where the code cannot read the text file and create an arraylist of objects from it. HouseListTester: import java.util.*; //Hard codes the criteria public class HouseListTester { static HouseList availableHouses; public static void main(String []args) { availableHouses = new HouseList("C:\\Users\\jvs34\\Downloads\\houses.txt"); Criteria c1 = new Criteria(1000, 500000, 100, 5000, 0, 10); Criteria c2 = new Criteria(1000, 100000, 500, 1200, 0, 3); Criteria c3 = new Criteria(100000, 200000, 1000, 2000, 2, 3); Criteria c4 = new Criteria(200000, 300000, 1500,...
I don't know why I keep getting the following error: AttributeError: 'tuple' object has no attribute...
I don't know why I keep getting the following error: AttributeError: 'tuple' object has no attribute 'size' I am using python in Anaconda. import numpy as np def information_gain(x_array, y_array): parent_entropy = entropy(x_array) split_dict = split(y_array) for val in split_dict.values(): freq = val.size / x_array.size child_entropy = entropy([x_array[i] for i in val]) parent_entropy -= child_entropy* freq return parent_entropy x = np.array([0, 1, 0, 1, 0, 1]) y = np.array([0, 1, 0, 1, 1, 1]) print(round(information_gain(x, y), 4)) x = np.array([0,...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT