2

My program does everything I want it to do, but AFTER it's done running I get this error that pops up. Looking through my code I cannot find where the error occurs.

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at simplestatistics.SimpleStatistics.main(SimpleStatistics.java:102)

Input -> Java Result: 1 package simplestatistics;

import java.util.*;
import java.lang.*;
import java.util.InputMismatchException;

public class SimpleStatistics {

public static double[] getUserInput(Scanner sc) {

    List<Double> inputList = new ArrayList<Double>();

    System.out.println("Please enter how many numbers you will be inputing");
    int numberOfInputs = sc.nextInt();

    for (int i = 0; i < numberOfInputs; i++) {
        System.out.println("Please enter a number");
        double userInput = sc.nextDouble();
        inputList.add(userInput);
    }
    sc.close();

    double[] arr = new double[inputList.size()];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = inputList.get(i);
    }
    return arr;
}

public static double arithmeticMean(double[] nums) {

    double mean = 0;
    double sum = 0;

    for (int i = 0; i < nums.length; i++) {
        sum = sum + nums[i];
    }
    mean = sum / nums.length;

    return mean;
}

public static double geometricMean(double[] nums) {

    double gm = 1.0;
    for (int i = 0; i < nums.length; i++) {
        gm *= nums[i];
    }
    gm = Math.pow(gm, 1.0 / (double) nums.length);
    return gm;
}

public static double[] minAndmax(double[] nums) {

    double min = nums[0];
    double max = nums[0];

    for (int i = 1; i < nums.length; i++) {
        if (nums[i] < min) {
            min = nums[i];
        } else if (nums[i] > max) {
            max = nums[i];
        } else {

        }
    }

    double[] minAndmax = {min, max};
    return minAndmax;
}

public static double[] scaleUp(double[] nums, int factor) {

    for (int i = 0; i < nums.length; i++) {
        nums[i] *= factor;
    }
    return nums;
}

public static double[] scaleDown(double[] nums, int factor) {

    for (int i = 0; i < nums.length; i++) {
        nums[i] /= factor;
    }
    return nums;
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    double[] input = {1, 2.8, 5.3, 100, -5, -6.5};

    System.out.println("Choose a option 1-6");

    boolean exit = false;
    while (!exit) {
        System.out.println();
        System.out.println("1) Arithmetic mean, 2) Geometric mean, 3)  minAndmax, 4)  Scale Up, 5)  Scale Down, 6)  Quit");
        System.out.print("Input -> ");
        int choice = sc.nextInt();
        System.out.println();

        switch (choice) {
            case 1: {
                // Arithmetic mean
                System.out.println("Arithmetic mean");
                System.out.println(arithmeticMean(getUserInput(sc)));
                break;
            }
            case 2: {
                // Geometric mean
                System.out.println("Geometric mean");
                System.out.println(arithmeticMean(getUserInput(sc)));
                break;
            }
            case 3: {
                // Min and max
                System.out.println("Min and Max");
                for (double i : minAndmax(getUserInput(sc))) {
                    System.out.print(i + ", ");
                }
                break;
            }
            case 4: {
                // Scale Up
                System.out.println("Scale Up");
                System.out.print("Please enter factor by which you want to scale -> ");
                int factor = sc.nextInt();
                for (double i : scaleUp(input, factor)) {
                    System.out.print(i + ", ");
                }
                break;
            }

            case 5: {
                // Scale Down
                System.out.println("Scale Down");
                System.out.print("Please enter factor by which you want to scale -> ");
                int factor = sc.nextInt();
                for (double i : scaleDown(input, factor)) {
                    System.out.print(i + ", ");
                }
                break;
            }

            case 6: {
                exit = true;
                break;
            }

        }
    }
}

}

Here's the link to my code

https://gist.github.com/Chunky1022/0775fba6692456ae1c8c#file-gistfile1-txt

2
  • 1
    sc.close(); ... ? I'd be careful closing resources you didn't create (ie resources that are passed to you) and closing the System.in is rarely a good idea Commented Apr 10, 2015 at 1:06
  • either you remove the sc.close() or put Scanner sc = new Scanner(System.in); inside while loop Commented Apr 10, 2015 at 1:11

1 Answer 1

2

At the first call of getUserInput you close the scanner.

public static double[] getUserInput(Scanner sc) {

    ...

    sc.close();

    ...
}

Simply remove the sc.close line.

If you really want to close it, then close it when you are sure you won't use this scanner anymore. However, I don't see why you would close System.in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.