A simple console-based input-output system for a fictional fish feeding model. I am taking inputs from the user and creating a 2-d array outlining relevant information.
I am having issues with one of my methods, namely this one:
public static int Intervallength(int Intervalindex){
int Intervalsize;
Scanner Input4 = new Scanner(System.in);
System.out.print("Enter length of Interval " + Intervalindex);
Intervalsize = Input4.nextInt();
Input4.close();
return Intervalsize;
}
I am receiving an error immediately after console prints the Interval length prompt:
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.nextInt(Unknown Source)
at Feedmodel.Intervallength(Feedmodel.java:56)
at Feedmodel.main(Feedmodel.java:38)
Not quite sure why this error occurs as I use the same syntax when I prompt user for other information and nothing goes wrong (this is before entering the loop). Anyone have an idea?
Here is the whole file for referencing.
import java.util.Scanner;
public class Feedmodel {
public static void main(String[] args){
double Initweight;
int Initpopulation;
int Intervals;
int Intervalsize;
double Etotal;
double Edigestion;
double ERetained;
double EMaintenance;
double Efecal;
double Efood = 18.5;
double Foodweight;
double Watertemp;
double Optweight;
Scanner Input = new Scanner(System.in);
System.out.print("Enter number of intervals: ");
Intervals = Input.nextInt();
double[][] Spreadsheet = new double[Intervals][6];
Scanner Input2 = new Scanner(System.in);
System.out.print("Enter initial trout weight: ");
Initweight = Input2.nextDouble();
Scanner Input3 = new Scanner(System.in);
System.out.print("Enter initial trout population: ");
Initpopulation = Input3.nextInt();
Input.close();
Input2.close();
Input3.close();
for (int i = 0; i < Intervals; i++){
Intervalsize = Intervallength(i + 1);
Spreadsheet[i][1] = Intervalsize;
Watertemp = Temperature(i + 1);
Spreadsheet[i][2] = Watertemp;
Initpopulation = Remainingfish(Initpopulation);
Spreadsheet[i][3] = Initpopulation;
Optweight = Fishweight(Initweight, Intervalsize, Watertemp);
Spreadsheet[i][4] = Optweight;
// Spreadsheet[i][5] = Feedkg();
}
}
public static int Intervallength(int Intervalindex){
int Intervalsize;
Scanner Input4 = new Scanner(System.in);
System.out.print("Enter length of Interval " + Intervalindex);
Intervalsize = Input4.nextInt();
Input4.close();
return Intervalsize;
}
public static double Temperature(int Intervalindex){
double Watertemp;
Scanner Input5 = new Scanner(System.in);
System.out.print("Enter water temperature of Interval " + Intervalindex);
Watertemp = Input5.nextDouble();
Input5.close();
return Watertemp;
}
public static int Remainingfish(int Initpopulation){
Initpopulation = (int)(Initpopulation * 0.978);
return Initpopulation;
}
public static double Fishweight(double Initweight, int Intervalsize, double Temperature){
int Fishweight;
double TGC = 0.191;
Fishweight = (int)(Math.pow((Math.cbrt(Initweight) + (TGC * Intervalsize * Temperature / 100)), 3));
return Fishweight;
}
}
Edit: I am confused because there is NO error in the same syntax outside of a method scope, only occurs inside the method. This is a situational error, I understand the point of searching.