Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.util.NoSuchElementException:使用java.util.Scanner时未找到任何行_Java_Java.util.scanner - Fatal编程技术网

java.util.NoSuchElementException:使用java.util.Scanner时未找到任何行

java.util.NoSuchElementException:使用java.util.Scanner时未找到任何行,java,java.util.scanner,Java,Java.util.scanner,我已经创建了两个类,一个用于主程序,另一个用于矩形的类本身,假设根据用户的输入创建两个矩形,打印矩形的信息,并用*打印矩形的形状,但在创建第二个矩形时,我遇到了这个错误: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Rectangle.input(Rectangle.java:74) at Pr

我已经创建了两个类,一个用于主程序,另一个用于矩形的类本身,假设根据用户的输入创建两个矩形,打印矩形的信息,并用*打印矩形的形状,但在创建第二个矩形时,我遇到了这个错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Rectangle.input(Rectangle.java:74)
at Program.main(Program.java:20)
import java.util.Scanner;

public class Rectangle {
    // Data Members for rectangle.
    private int width;
    private int height;
    public String color;
    public int xPos;
    public int yPos;

    //  Initialization.. 
    public void init(int width, int height, String color, int xPos, int yPos) {
        this.width = width;
        this.height = height;
        this.color = color;
        this.xPos = xPos;
        this.yPos = yPos;
    }
    //  Print all Data Members.
    public void printInfo() { 
        System.out.println("Width: " + width + ",Height: " + height
                + ",Color: " + color + ",X position: " + xPos + ",Y position: "
                + yPos);
    }

    //  Setter (width)
    public void setWidth(int width){
        if(width >= 0 ){
            this.width = width;
        }
    }
    //  Getter (width)
    public int getWidth(){
        return width;
    }

    //  Setter (height)
    public void setHeight(int height){
        if(height >= 0){
            this.height = height;
        }
    }
    //  Getter (height)
    public int getHeight(){
        return height;
    }

    public void starsRectangle(){
        for(int i=0; i<getHeight(); i++){
            for(int j=0; j<getWidth(); j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public void input(){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter color");
        String inputC = s.nextLine();
        System.out.println("Enter width");
        int inputW = s.nextInt();
        System.out.println("Enter height");
        int inputH = s.nextInt();
        System.out.println("Enter x position");
        int inputXPos = s.nextInt();
        System.out.println("Enter y position");
        int inputYpos = s.nextInt();
        setWidth(inputW);
        setHeight(inputH);
        color = inputC;
        xPos = inputXPos;
        yPos = inputYpos;
        s.close();
    }
}
这是矩形的类:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Rectangle.input(Rectangle.java:74)
at Program.main(Program.java:20)
import java.util.Scanner;

public class Rectangle {
    // Data Members for rectangle.
    private int width;
    private int height;
    public String color;
    public int xPos;
    public int yPos;

    //  Initialization.. 
    public void init(int width, int height, String color, int xPos, int yPos) {
        this.width = width;
        this.height = height;
        this.color = color;
        this.xPos = xPos;
        this.yPos = yPos;
    }
    //  Print all Data Members.
    public void printInfo() { 
        System.out.println("Width: " + width + ",Height: " + height
                + ",Color: " + color + ",X position: " + xPos + ",Y position: "
                + yPos);
    }

    //  Setter (width)
    public void setWidth(int width){
        if(width >= 0 ){
            this.width = width;
        }
    }
    //  Getter (width)
    public int getWidth(){
        return width;
    }

    //  Setter (height)
    public void setHeight(int height){
        if(height >= 0){
            this.height = height;
        }
    }
    //  Getter (height)
    public int getHeight(){
        return height;
    }

    public void starsRectangle(){
        for(int i=0; i<getHeight(); i++){
            for(int j=0; j<getWidth(); j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public void input(){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter color");
        String inputC = s.nextLine();
        System.out.println("Enter width");
        int inputW = s.nextInt();
        System.out.println("Enter height");
        int inputH = s.nextInt();
        System.out.println("Enter x position");
        int inputXPos = s.nextInt();
        System.out.println("Enter y position");
        int inputYpos = s.nextInt();
        setWidth(inputW);
        setHeight(inputH);
        color = inputC;
        xPos = inputXPos;
        yPos = inputYpos;
        s.close();
    }
}
你可以用

 while(s.hasNextLine()){..}
以避免此错误。

您应该创建
Scanner s=新的扫描仪(System.in)
作为实例变量或类变量。你可以在课堂上的任何地方使用它。编写一个close()方法,在退出应用程序之前关闭扫描程序流


不要每次调用并关闭方法时都创建扫描仪对象。而是创建一次,关闭一次流。

这并不能真正解决问题。在输入法中关闭扫描仪也会关闭System.in,因此,在为第二个矩形创建新扫描仪时,您无法再从控制台读取。可能存在重复的