文件中的java填充/未填充矩形
该项目应该读取一个文件,其中文件的第一行表示要制作多少个矩形,其余的是尺寸以及是否填充。假设矩形类带有toString来格式化矩形。设置变量numLines时,while循环的第一行出现输入不匹配错误,需要一些帮助
public static void main(String[] args) {
int columns = 0;
int rows = 0;
int numLines = 1;
String fill;
boolean filled = false;
//first line of file creates array size
Rectangle[] rectangle = new Rectangle[numLines];
Scanner inFile = new Scanner("rectangle.txt");
//reads file
while (inFile.hasNext()) {
numLines = inFile.nextInt();
for (int i=0; i<numLines; i++) {
columns = inFile.nextInt();
rows = inFile.nextInt();
fill = inFile.nextLine();
if (fill.equals("filled")) {
filled = true;
}
Rectangle rec = new Rectangle(rows, columns, filled);
rectangle = new Rectangle[numLines];
rectangle[i] = rec;
}
//example of file being read
6
6 3 filled
3 6 unfilled
4 4 filled
6 6 unfilled
9 4 filled
4 8 unfilled
# 1 楼答案
您正在使用文本字符串而不是文件创建扫描仪。文本字符串“rectangle.txt”不是int值
如果要读取文件,则需要将文件指定为参数: