Scanner Class Error

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


The problem is in your enterGradeNames() method with:

input.next()

You have to first call input.hasNext() ;

From documentation:

Throws: NoSuchElementException - if no more tokens are available.

EDIT: as per comments

I am unable to reproduce the problem, but there are many unnecessary lines in your code, so try running this edited code and see whether it changes anything.

public class Grades {
private int len;
private String [] gradeNames;
private int [] gradeArray;

private int enterGradeNames(){
    Scanner 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());
    }
    return 0;
}

public Grades(int length){
    this.len = length;
    gradeArray = new int[len];
    gradeNames = new String[len];
}

It's not generally good choice to call non-static methods inside constructor as the object isn't finished yet. You could do this in a (factory) method instead:

public static Grades buildGrades(){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter size:");
    int size = s.nextInt();
    Grades grades = new Grades(size);
    grades.enterGradeNames();

    return grades;
}

EDIT2:

I searched a bit and the problem might be with your closing of the Scanner . Because if you call close on the Scanner , it will look whether its stream implements Closeable and if so it will close it as well. I never thought System.in would be closeable, but it is.

So the best option? Possibly use one Scanner for the whole program OR just don't close it if you dont want its stream to be closed. More can be read here.


试试这个:

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];
enterGradeNames();
input.close();
}

I removed from your "Grades" Constructor the input reference because it is useless to do it twice (at enterGradeNames you initilize it again). The problem was you closed the resource at the constructor and then tried to recreate it again.

public Grades(){
    len = 0;
    System.out.println("Enter the size of the grade array to be created");
    len = 4;
    gradeArray = new int[len];
    gradeNames = new String[len];
    enterGradeNames();
}
链接地址: http://www.djcxy.com/p/96092.html

上一篇: Java尝试读取文本文件时遇到运行时错误

下一篇: 扫描仪类错误