【发布时间】:2018-02-12 03:46:46
【问题描述】:
我正在尝试使用构造函数从文件创建对象,该文件应包含(在第一行)一个字符串格式的 Int,这意味着 MD 数组的行数,然后具有一个空格,后跟另一个字符串格式的 Int。我试图“抓取”这两个字符串,将它们解析为一个 int,然后使用我“抓取”的这两个 int 实例化 MD 数组。我只是不太确定我哪里出错了,因为我刚刚开始在我的编码中使用文件 I/O。这是我的代码。
public SeatingChart(File file) throws FileNotFoundException, DataFormatException, IOException
{
Scanner scan = new Scanner(file);
int rows = 0;
int columns = 0;
String rowStr = "";
String colStr = "";
if (scan.hasNext())
{
rowStr = scan.next();
colStr = scan.next();
}
rows = Integer.parseInt(rowStr);
columns = Integer.parseInt(colStr);
seats = new Student[rows][columns];
scan.close();
}
任何帮助将不胜感激:)
【问题讨论】:
-
Scanner.nextInt()? -
@CardinalSystem 它在文件中时不是 int,或者至少据我所知不是。
-
那么你怎么知道你错了?
-
@immibis 有一个 jar 文件提供给我的班级,我应该将文件放入其中,每次放入文件时都会出错。
-
@JamwadAmadeusDixon 我今天开车去商店,因为我需要买一些牛奶,因为我用完了最后一点牛奶做奶油炒鸡蛋,但是车撞了,我做错了什么?
标签: java file-io java.util.scanner