1

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.

user3072912
  • 64
  • 1
  • 2
  • 10
  • What happens if you remove `Input4.close()` ? I think it is also closing the source but I don't think it would be this exception in that case – Dici Oct 02 '14 at 23:00
  • Do you know why such exception is thrown ? http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt() – StackFlowed Oct 02 '14 at 23:02
  • You should consider just reusing the input variable, instead of creating new scanners on System.in. And @Dici is right... your closing System.in, then youre going to read from it? Also, your inputN variables within your other methods are in a different scope than main(), so you can can name them just input, and it will work. – Mark W Oct 02 '14 at 23:05
  • Closing the scanner doesn't make a difference. Like I said, before entering the loop I do the same thing and no error occurs. I changed to this while(!Input4.hasNextInt()){ } Intervalsize = Input4.nextInt(); and now the program does not crash however the compiler seems to stand still after taking the input. – user3072912 Oct 02 '14 at 23:07

0 Answers0