Я создал 2 класса один для основной программы и один для самого класса для прямоугольника, который предположил бы создать 2 прямоугольника от ввода от пользователя печатает информацию прямоугольника и выводит форму прямоугольника с *, но я получаю эту ошибку, когда я создаю второй прямоугольник:java.util.NoSuchElementException: строка не найдена при использовании 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 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();
}
}
это класс для основной программы:
public class Program {
public static void main(String[] args) {
Rectangle rec1 = new Rectangle(); // Reference1 + object
rec1.init(5, 3, "BLUE", 90, 50);
rec1.printInfo();
rec1.starsRectangle();
System.out.println("--------");
rec1.input();
rec1.starsRectangle();
System.out.println("--------");
Rectangle rec2 = new Rectangle(); // Reference2 + object
rec2.input();
rec2.printInfo();
rec2.starsRectangle();
}
}
Возможный дубликат http://stackoverflow.com/questions/7348377/reading-system-in-multiple-times-in-java-causes-ioexception – DeiAndrei