Aureliux
Aureliux

Reputation: 53

Java cube program having issues on second and third instances

This is my program; the point of it is to take a side value from the user and output the surface area and volume. I am checking for incorrect input into the program, like inputting a letter instead of a number. It works when I only use one instance but on the second and third instances I receive an error. I am not sure what my issue is here.

import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;

public class cube

{
    private double side;

public double getLength(){

    Scanner sc = new Scanner(System.in);

while(true){
    try{
        System.out.println("Please enter an integer: ");
        side = sc.nextDouble();
        break;

    }
    catch(InputMismatchException a){

        System.err.println("\nWrong character");
        sc.next();
        continue;
    }
}   
    sc.close();
    return side;
}

/*
 * Calculate the Surface Are of the cube base on the user side input
 */
public double calculateSurfaceArea(){

    return 6 * side * side;
}

/*
 * Calculate the volume of the cube base on the user side input
 */
public double calculateVolume(){

    return side * side * side;
}


/**
 * main() -- creates an instance of Cube and tests it
 */
public static void main(String args[]) throws IOException
{

    // HINT: input the side from the keyboard and check for errors and exceptions
    cube cube1 = new cube();

    // Print the test results
    System.out.println("\nSide length of cube1 is " + cube1.getLength());
    System.out.println("Surface Area of cube1 is " + cube1.calculateSurfaceArea ());
    System.out.println("Volume of cube1 is " +  cube1.calculateVolume());
    // Hint - add two more cube instances

    cube cube2 = new cube();

    // Print the test results
    System.out.println("\nSide length of cube2 is " + cube2.getLength());
    System.out.println("Surface Area of cube2 is " + cube2.calculateSurfaceArea ());
    System.out.println("Volume of cube2 is " +  cube2.calculateVolume());

    cube cube3 = new cube();

    // Print the test results
    System.out.println("\nSide length of cube3 is " + cube3.getLength());
    System.out.println("Surface Area of cube3 is " + cube3.calculateSurfaceArea ());
    System.out.println("Volume of cube3 is " +  cube3.calculateVolume());
} // main() } // Cube

The error is:

 Please enter an integer: 
    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 folder.name.........(cube.java:18)
        at folder.name.........(cube.java:68)

Upvotes: 1

Views: 70

Answers (1)

Makoto
Makoto

Reputation: 106460

You're closing the System.in stream here:

sc.close();

This prevents all other instances from ever getting access to that stream and reading from it, which is why you're getting the NoSuchElementException. Remove that line and you should be good.

As a general rule, never close System streams (in, out, or err). Other parts of your application may depend on them, if they're closed, you'll get this cryptic exception every time.

Upvotes: 1

Related Questions