I'm doing some homework where we have to use the Scanner class. I was able to use it to read in a String, an int, and a float. When I moved to the next phase (the class below) I suddenly am not able to use scanner the way I had before. I did indeed close any other scanner object I created and opened. Thank you for any help.
Why does this code: (nextLine() also does not work)
import java.io.*;
import java.util.Scanner;
public class Grades {
private int len;
private String [] gradeNames;
private int [] gradeArray;
private int enterGradeNames(){
Scanner input = null;
input = new Scanner(System.in);
for (int i = 0; i < len; ++i){
System.out.println("Enter the type of grades you will be reporting: (" +
(i + 1) + " of " + gradeArray.length + ")" );
gradeNames[i] = new String(input.next() );
}
input.close();
return 0;
}
protected int displayGradeNames(){
System.out.println("Type of Grades tracking");
for (int i = 0; i < len; ++i)
System.out.println(gradeNames[i]);
return 0;
};
public Grades(){
len = 0;
Scanner input = null;
input = new Scanner(System.in);
System.out.println("Enter the size of the grade array to be created");
len = 4;
gradeArray = new int[len];
gradeNames = new String[len];
input.close();
enterGradeNames();
}
}
give me this errror:
Enter the type of grades you will be reporting: (1 of 4)
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Grades.enterGradeNames(Grades.java:14)
at Grades.(Grades.java:34)
at Demo1.main(Demo1.java:32)
** Oh.. i should mention that it doesn't even give the option to input data before throwing the error