我正在尝试将文本文件用作从用户那里收集的一些数据的数据库,这是我正在使用的构造函数
public DatabaseOperations(String name,double newWeight) throws FileNotFoundException, ParseException{
f= new File (removeSpaces(name)+".txt");
if (!f.exists()){
System.out.println("This user dosen't exist");
return;
}
try {
out= new PrintWriter (f);
} catch (FileNotFoundException e) {
System.out.println("Error, You don't say");
}
// This section is used to initialize a new object from Customer with values in the excising file
cust = new Customer();
in=new Scanner(f);
in.next();
in.next();
cust.setName(in.next());
in.next();
in.next();
in.next();
cust.setStartingDate(in.next());
in.next();
in.next();
cust.setAge(Integer.parseInt(in.next()));
in.next();
in.next();
in.next();
cust.setStartingWeight(Double.parseDouble(in.next()));
in.next();
in.next();
cust.setHeight(Double.parseDouble(in.next()));
in.next();
in.next();
in.next();
cust.setBMI(Double.parseDouble(in.next()));
in.next();
in.next();
in.next();
cust.setTargetWeight(Double.parseDouble(in.next()));
in.next();
in.next();
in.next();
cust.setTargetDate(in.next());
//----------------------------------------------------------------
// check this for errors !!!
cust.setWeight(newWeight);
out.println(form.format(cust.getCurrentDate())+"\t"+df.format(cust.getWeight())+"\t\t"+cust.getBMI()+"\t\t"+cust.percentDone()+"\t"+cust.timePassed());
out.close();
}
这些很多 in.next() 是用来跳过一些我不需要的数据,我使用的文件结构是这样的
Name :kkkk Started at :06/12/2012
Age :19 Starting Weight :85.0
Height :1.86 Starting BMI :24.57
Target Weight :75.5 Target Date :15/12/2012
问题是编译器抛出 NoSuchElementException 并将我指向第一个 (in.next()) 另一个问题是,一旦我调用此构造函数,文件就会变为空!!!