Java Error after program runs -
my program want do, after it's done running error pops up. looking through code cannot find error occurs.
exception in thread "main" java.lang.illegalstateexception: scanner closed @ java.util.scanner.ensureopen(scanner.java:1070) @ java.util.scanner.next(scanner.java:1465) @ java.util.scanner.nextint(scanner.java:2117) @ java.util.scanner.nextint(scanner.java:2076) @ 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 inputing"); int numberofinputs = sc.nextint(); (int = 0; < numberofinputs; i++) { system.out.println("please enter number"); double userinput = sc.nextdouble(); inputlist.add(userinput); } sc.close(); double[] arr = new double[inputlist.size()]; (int = 0; < arr.length; i++) { arr[i] = inputlist.get(i); } return arr; } public static double arithmeticmean(double[] nums) { double mean = 0; double sum = 0; (int = 0; < nums.length; i++) { sum = sum + nums[i]; } mean = sum / nums.length; return mean; } public static double geometricmean(double[] nums) { double gm = 1.0; (int = 0; < 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]; (int = 1; < 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) { (int = 0; < nums.length; i++) { nums[i] *= factor; } return nums; } public static double[] scaledown(double[] nums, int factor) { (int = 0; < 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 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 , max system.out.println("min , max"); (double : minandmax(getuserinput(sc))) { system.out.print(i + ", "); } break; } case 4: { // scale system.out.println("scale up"); system.out.print("please enter factor want scale -> "); int factor = sc.nextint(); (double : scaleup(input, factor)) { system.out.print(i + ", "); } break; } case 5: { // scale down system.out.println("scale down"); system.out.print("please enter factor want scale -> "); int factor = sc.nextint(); (double : scaledown(input, factor)) { system.out.print(i + ", "); } break; } case 6: { exit = true; break; } } } }
}
here's link code
https://gist.github.com/chunky1022/0775fba6692456ae1c8c#file-gistfile1-txt
at first call of getuserinput
close scanner.
public static double[] getuserinput(scanner sc) { ... sc.close(); ... }
simply remove sc.close
line.
if want close it, close when sure won't use scanner anymore. however, don't see why close system.in
.