1

Alright so this is my first time posting.

I'm trying to create a box using another class that creates a rectangle. But when I try to run, I get to input the values for height and width but right after I try to input depth this error pops up.

Thanks in advance for any help.

Console:

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;
}
}

Rectangle class:

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;
}
}

Main:

public class Steg4_lab02 {

public static void main(String[] args) {

    new Box();

}

}
Carl
  • 11
  • 2
  • Don't close the `Scanner` if you are not done with it. – Murat Karagöz Jul 08 '16 at 09:11
  • I recommend, that the next time you ask a question you have more explanatory variable names and the output strings are in English. You should help the people help you. This is generally a better way to code. If you want the output strings to use a different language i suggest you off start with English and change them just before the first public version. – nick zoum Jul 08 '16 at 09:13
  • I consider it a bad design to collect the user input in the Constructors. – Fildor Jul 08 '16 at 09:15

1 Answers1

2

You shouldn't instantiate multiple Scanners with System.in, and it's a big no-no to close a Scanner instantiated with System.in (basically closing the STDIN stream) and then creating a new Scanner with System.in.

Rectangle should have a constructor that either takes a Scanner or a constructor that takes a width and height. Then either use the passed in Scanner to collect user input or collect user input in main() and create a Rectangle with the width and height collected from the user.

As Fildor stated in the comments, the Box and Rectangle classes should not be responsible for gathering user input.

Jonny Henly
  • 3,725
  • 4
  • 23
  • 41