尝试输入时没有这样的元素异常

时间:2016-07-08 09:05:16

标签: java

好吧,这是我第一次发帖。

我正在尝试使用另一个创建矩形的类创建一个框。但是当我尝试运行时,我会输入高度和宽度的值,但在我尝试输入深度后,会弹出此错误。

提前感谢您的帮助。

控制台:

Ange rektangelns bredd: 
10
Ange rektangelns höjd: 
10
En rektangelns med bredden 10 och höjden 10 ger arean 100
Ange rektangelns djup: 
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at cj222qu.Box.<init>(Box.java:18)
at cj222qu.Steg4_lab02.main(Steg4_lab02.java:7)

Box class:

import java.util.Scanner;

public class Box extends Rectangle {

private int depth;

public Box() {
    Scanner hej = new Scanner(System.in);

    String dep = null;
    boolean go3 = true;

    while (go3) {
        try {
            System.out.println("Ange rektangelns djup: ");
            dep = hej.next();
            Integer.parseInt(dep);
            go3 = false;
        } catch (NumberFormatException e) {
            System.out.println("Ett fel har inträffat! Ange värdet som ett tal");
            go3 = true;
        }
    }

    //new Box(getWidth(), getHeight(), Integer.parseInt(dep));

    hej.close();
}

public Box(int width, int height, int depth) {

    setDepth(depth);

    System.out.println(toString());
}

public String toString() {
    StringBuilder result = new StringBuilder();

    result.append("En låda med bredden " + getWidth() + ", höjden " + getHeight() + " och djupet " + ".");
    result.append("Lådans volym är " + computeVolume() + ".");
    result.append("Lådans mantelarea är " + computeArea() + ".");
    return result.toString();
}

public int computeVolume() {
    int volume = 0;

    volume = getWidth() * getHeight() * getDepth();

    return volume;
}

public int computeArea() {
    int mantelarea = 0;

    mantelarea = getDepth() * getWidth() * 2 + getDepth() * getHeight() * 2 + getWidth() * getHeight() * 2;

    return mantelarea;
}

public int getDepth()
{
    return depth;
}

public void setDepth(int d)
{
    depth = d;
}
}

矩形类:

import java.util.Scanner;

public class Rectangle {


private int height;
private int width;

public Rectangle(int width, int height)
{
    setHeight(height);
    setWidth(width);

    System.out.println(toString());
}



@Override
public String toString() {
    StringBuilder result = new StringBuilder();

    result.append("En rektangelns med bredden " + width + " och höjden " + height + " ger arean " + computeArea());
    return result.toString();
}



public Rectangle()
{
    Scanner keyboard = new Scanner(System.in);


    String w = null;
    String h = null;
    boolean go1 = true;
    boolean go2 = true;

    while (go1) {
        try {
            System.out.println("Ange rektangelns bredd: ");
            w = keyboard.next();
            Integer.parseInt(w);
            go1 = false;
        } catch (NumberFormatException e) {
            System.out.println("Ett fel har inträffat! Ange värdet som ett tal");
            go1 = true;
        }

    } 

    while (go2) {
        try {
            System.out.println("Ange rektangelns höjd: ");
            h = keyboard.next();
            Integer.parseInt(h);
            go2 = false;
        } catch (NumberFormatException e) {
                System.out.println("Ett fel har inträffat! Ange värdet som ett tal");
                go2 = true;
        }
    }

    new Rectangle(Integer.parseInt(w), Integer.parseInt(h));


    keyboard.close();
}

public int computeArea()
{
    int area = 0;

    area = getHeight() * getWidth();

    return area;
}

public int getHeight()
{
    return height;
}

public int getWidth()
{
    return width;
}

public void setHeight(int h)
{
    height = h;
}

public void setWidth(int w)
{
    width = w;
}
}

主:

public class Steg4_lab02 {

public static void main(String[] args) {

    new Box();

}

}

1 个答案:

答案 0 :(得分:1)

您不应该使用Scanners实例化多个System.in,并且关闭使用Scanner实例化的System.in是一个很大的禁忌 (基本上关闭STDIN流),然后使用Scanner创建新的System.in

Rectangle应该有一个构造函数,它可以采用Scanner或者采用宽度和高度的构造函数。然后使用传入的Scanner收集用户输入或收集main()中的用户输入,并创建一个Rectangle,其中包含从用户收集的宽度和高度。

正如Fildor在评论中所说,BoxRectangle类不应负责收集用户输入。