我必须检索三个输入,一个整数、一个字符串和一个双精度数;这就是我想我会怎么做[注意:包含空格的字符串]
record.setAccountNumber(input.nextInt());
record.setName(input.nextLine());
record.setBalance(input.nextDouble());
我试图替换 input.nextLine() 在
record.setName(input.nextLine());
使用输入 input.next(),由于 InputMisMatchException 但问题仍未解决。抛出错误是因为可能将双精度值分配给新的行值[这是我认为不确定的] 是否有办法检索包含空格的字符串并能够完成我必须输入的三个输入同时。谢谢
注意:我找不到与此相关的任何问题让我添加发生错误的整个方法
public void addAccountRecords(){
AccountRecord record = new AccountRecord();
input = new Scanner (System.in);
try {
output = new Formatter("oldmast.txt");
} catch (FileNotFoundException e) {
System.err.println("Error creating or opening the file");
e.printStackTrace();
System.exit(1);
} catch (SecurityException e){
System.err.println("No write access to this file");
e.printStackTrace();
System.exit(1);
}
System.out.println("Enter respectively an account number\n"+
"a name of owner\n"+"the balance");
while ( input.hasNext()){
try{
record.setAccountNumber(input.nextInt());
record.setName(input.nextLine());
record.setBalance(input.nextDouble());
if (record.getBalance() >0){
output.format("%d\t%s\t%,.2f%n",record.getAccountNumber(),
record.getName(),record.getBalance());
record.setCount((record.getCount()+1));
}else
System.err.println("The balance should be greater than zero");
}catch (NoSuchElementException e){
System.err.println("Invalid input, please try again");
e.printStackTrace();
input.nextLine();
}catch (FormatterClosedException e){
System.err.println("Error writing to File");
e.printStackTrace();
return;
}
System.out.println("Enter respectively an account number\n"+
"a name of owner\n"+"the balance\n or End of file marker <ctrl> z");
}//end while
output.close();
input.close();
}//end AddAccountRecords