I wrote a program with a separate class but I keep getting the same error right after the user inputs the three sides.
The main code is:
package interactiveTriangleWithAClass;
public class InteractiveTriangleProgramClass
{ public static void main (String [] args) throws Exception
{ IAclass nums = new IAclass();
double perimeter;
double area;
explain();
nums.getNumbers();
perimeter = nums.calcPer();
area = nums.calcArea();
outputResults(nums, perimeter, area); }
public static void explain()
{ System.out.println("This program calculates the perimeter ");
System.out.print("and the area of 3 sides for a triangle. ");
System.out.println("The 3 numbers are entered by the user.\n"
+ "The output is the 3 numbers, the perimeter and the area.\n"
+ "A seperate class is used.\n"
+ "Program written by Derek Michel"); }
public static void outputResults(IAclass nums, double area, double perimeter) throws Exception
{ nums.getNumbers();
System.out.println();
System.out.println("The perimeter is " + perimeter);
System.out.print(" and the area is " + area); }
}
The class is:
package interactiveTriangleWithAClass;
import java.util.*;
public class IAclass {
private double num1, num2, num3;
public IAclass() {
num1 = num2 = num3 = 3;
}
public IAclass(double value1, double value2, double value3) {
num1 = value1;
num2 = value2;
num3 = value3;
}
public void getNumbers() throws Exception {
Scanner console = new Scanner(System. in );
System.out.println("Enter the first side.");
num1 = console.nextDouble();
System.out.println("Enter the second side.");
num2 = console.nextDouble();
System.out.println("Enter the third side.");
num3 = console.nextDouble();
console.close();
while (num1 <= 0) {
System.err.print("Error. Side one must be greater than 0.");
System.out.println("Enter side one again.");
num1 = console.nextDouble();
}
while (num2 <= 0) {
System.err.print("Error. Side two must be greater than 0.");
System.out.println("Enter side two again.");
num2 = console.nextDouble();
}
while (num3 <= 0) {
System.err.print("Error. Side three must be greater than 0.");
System.out.println("Enter side three again.");
num3 = console.nextDouble();
}
if (num1 + num2 < num3) {
System.out.println("Based on the sides inputed, your triangle ");
System.out.print("is impossible to create.");
System.exit(0);
}
if (num2 + num3 < num1) {
System.out.println("Based on the sides inputed, your triangle ");
System.out.print("is impossible to create.");
System.exit(0);
}
if (num1 + num3 < num2) {
System.out.println("Based on the sides inputed, your triangle ");
System.out.print("is impossible to create.");
System.exit(0);
}
}
public double calcPer() {
return (num1 + num2 + num3);
}
public double calcArea() {
double s;
s = (num1 + num2 + num3) / 2;
return (Math.sqrt(s * (s - num1) * (s - num2) * (s - num3)));
}
public void outputNumbers() {
System.out.print("The sides are " + num1 + ", " + num2);
System.out.print(" and" + num3);
}
}
The console reads this after running and after 3 sides are inputed:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at interactiveTriangleWithAClass.IAclass.getNumbers(IAclass.java:48)
at interactiveTriangleWithAClass.InteractiveTriangleProgramClass.outputResults(InteractiveTriangleProgramClass.java:54)
at interactiveTriangleWithAClass.InteractiveTriangleProgramClass.main(InteractiveTriangleProgramClass.java:41)