I am implementing a program which calculate the area and perimeter of a circle and a rectangle. The radius (of circle), width, height (of rectangle) get from user. The problem is: after I type in the radius parameter, the console pop out the message:
Exception in thread "main" java.util.NoSuchElementException at...
Main.java is located in main package, Circle.java and Rectangle.java are located in shapes package.
Main.java:
package main;
import shapes.Circle;
import shapes.Rectangle;
//import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Circle cir = new Circle();
Rectangle rec = new Rectangle();
cir.printArea();
cir.printPerimeter();
rec.printArea();
rec.printPerimeter();
}
}
Circle.java:
package shapes;
import java.util.Scanner;
public class Circle {
final double PI = 3.14;
double radius;
double area, perimeter;
public Circle(double radius) {
this.radius = radius;
area = PI*radius*radius;
perimeter = 2*PI*radius;
}
public Circle() {
System.out.print("radius = ");
Scanner scan = new Scanner(System.in);
radius = scan.nextDouble();
scan.close();
area = PI*radius*radius;
perimeter = 2*PI*radius;
}
public void printArea() {
System.out.println("Area of circle = " area);
}
public void printPerimeter() {
System.out.println("Perimeter of circle = " perimeter);
}
}
Rectangle.java:
package shapes;
import java.util.Scanner;
public class Rectangle {
public double length, width;
double area, perimeter;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
area = length * width;
perimeter = (length width) * 2;
}
public Rectangle() {
Scanner scan = new Scanner(System.in);
System.out.print("width = ");
width = scan.nextDouble();
System.out.print("length = ");
length = scan.nextDouble();
scan.close();
area = length * width;
perimeter = (length width) * 2;
}
public void printArea() {
System.out.println("Area of rectangle = " area);
}
public void printPerimeter() {
System.out.println("Perimeter of rectangle = " perimeter);
}
}
Why does this happen and how to fix it?
CodePudding user response:
- Your Scanner should be in the main class and be static you only use one and no need more.
- Circle and Rectangle have some base methodes which should be outsorced so use an interface or in this case better a abstract class like Geoform as Superclass.
- And don't close the scanner, it is simply not necessary.
Main:
public class Main {
private static final Scanner scan = new Scanner(System.in);
private static Circle createCircle(){
System.out.print("radius = ");
double radius = scan.nextDouble();
return new Circle(radius);
}
private static Rectangle createRectangle() {
System.out.print("width = ");
double width = scan.nextDouble();
System.out.print("length = ");
double length = scan.nextDouble();
return new Rectangle(width, length);
}
public static void main(String[] args) {
Circle cir = createCircle();
Rectangle rec = createRectangle();
cir.printArea();
cir.printPerimeter();
rec.printArea();
rec.printPerimeter();
}
}
Geoform:
public abstract class Geoform {
final double area;
final double perimeter;
public Geoform(double area, double perimeter) {
this.area = area;
this.perimeter = perimeter;
}
public void printArea() {
System.out.println("Area = " area);
}
public void printPerimeter() {
System.out.println("Perimeter = " perimeter);
}
}
Circle:
public class Circle extends Geoform{
public static final double PI = 3.14;
private final double radius;
public Circle(double radius) {
super(PI*radius*radius, 2*PI*radius);
this.radius = radius;
}
public double getRadius() {
return radius;
}
}
Rectangle:
public class Rectangle extends Geoform {
private final double length, width;
public Rectangle(double width, double length) {
super(length * width, (length width) * 2);
this.width = width;
this.length = length;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
}
CodePudding user response:
you have a few problems..
first, you need to learn how to write more clear code, like: circle, instead of "cir". like Circle circle=new Circle(); second, we usually don't use Scanner inside a constructor try to use inside the method. or create a method for input read. third, don't close the scanner. (most probably this is the cause of the mistake). try to comment your code..