public Ticketmachine(boolean openAutomat, String nameMachine, int ticketprice)
{
if(openMachine == true){
Scanner reader = new Scanner(nameMachine+".txt");
String x = reader.nextLine();
String y = reader.nextLine();
String z = reader.nextLine();
int u = Integer.parseInt(x);
int v = Integer.parseInt(y);
int w = Integer.parseInt(z);
price = u;
paid = v;
sum = w;
}
每次打开一个新类并将openMachine设置为true时, 它应该从.txt文件的前三行中取三个数字 并将它们放入变量x,y和z中。 这些将被转换为数据类型整数然后 他们将被纳入变量价格,支付和总和。 现在,当我使openMachine为true时会发生什么, 是它显示了以下错误:
java.util.NoSuchElementException:找不到行
并标志着第九行:
int v = Integer.parseInt(y);
如果有人能告诉我答案,那就太好了:D
答案 0 :(得分:1)
如果使用String构造扫描程序,则它使用该String作为源。您需要将File对象传递给Scanner构造函数。
Scanner reader = new Scanner(new File(nameMachine+".txt"));