Stuck at “Exception in thread ”main\" java.util.No

2020-04-28 06:24发布

问题:

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)

回答1:

Try removing the following line:

console.close();

You can't read from console once you've closed it.

You almost certainly shouldn't explicitly close your Scanner: doing so will close System.in. It is not a good practice to close a stream that you haven't opened yourself, since some other code may rely on the stream being open, and throw an exception when it isn't.

Debugging such a problem - where the exception is potentially thrown a long way from the cause - is very tricky.



回答2:

Scanner#nextDouble throws NoSuchElementException

if the input is exhausted

The way do avoid it, and the correct way to use scanner generally (also for iterators) is to first check if there is such an input available. Try:

if (scanner.hasNextDouble())
    scanner.nextDouble();

If the call returns false then there is simply no double to read.