我真的很执着于Java的工作。只是一个初学者,我需要编写一个捐赠管理系统,该系统需要读取2个文件(捐赠者记录和说明),我需要阅读它们并验证捐赠者记录,然后print写入经过验证的记录并以某些toString格式打印。 / p>
需要读取以下记录:(字段可以按任何顺序排列
interface IState{
users : Account[];
user: Account;
editingIndex: number | null;
}
// In constructor
constructor(props:any){
super(props);
this.state= {
users: [],
user: {
name: '',
email: '',
phone: '',
},
editingIndex: null
}
}
showDetails = (i:number) => {
this.setState ({user: this.state.users[i], editingIndex: i});
console.log(i);
}
handleChange = ( event: React.ChangeEvent<HTMLInputElement>) => {
let user = {...this.state.user,
[event.currentTarget.name]:event.currentTarget.value};
this.setState({user});
// If we currently editing existing item, update it in array
if (this.state.editingIndex !== null) {
let users = [...this.state.users];
users[this.state.editingIndex] = user;
this.setState({users});
}
}
removeAccount = (i:number) => {
let users = [...this.state.users];
// If we're going to delete existing item which we've been editing, set editingIndex to null, to specify that editing ends
if (this.state.editingIndex === i)
this.setState({user: {name: '', email: '', phone: ''}, editingIndex: null});
users.splice(i,1);
this.setState({users},()=>{console.log('setting the data')});
}
onAdd = () => {
e.preventDefault();
// If we NOT editing, but adding new editingIndex will be null so add user to users array. If we editing existing element it's no need to add it once again.
if (this.state.editingIndex === null)
this.setState({ users: [...this.state.users, this.state.user] });
this.setState ({ editingIndex: null,
user: { name:'', email: '', phone: ''}
},()=>{console.log('adding')});
}
// render will have no change
我已经创建了3个扫描仪, 1.扫描记录文件 2.扫描每一行 3.扫描关键字和值
但是随后我得到了结果文件,其中每个字段都创建了一个捐赠者对象,而不是将所有字段都放在一个对象中。
输出如下:
phone 02111111
name Posephine Bloggs
birthday 01-06-1980
address 1 Grace Street, Lane Cove, NSW
postcode 2066
recipient dog care, the disabled
donation 100, 300
address 102 Smith St, Summer hill, NSW
postcode 2130
name Posephine Esmerelda Bloggs
birthday 13-05-1960
phone 11222009
recipient animal protection, lifecare
donation 50, 200
答案 0 :(得分:1)
您的错误在这里:
while(scanRecord.hasNextLine()) {
donator a = new donator();
String line = scanRecord.nextLine();
这意味着您将为阅读的每一行创建一个新的捐赠者,而您应该在阅读空白一行时创建一个新的捐赠者。
解决此问题的一种方法是将a
的声明移出while循环,并且仅在读取的行为空白时才初始化a
。您还应该将捐赠者添加到else分支的列表中。
donator a = new donator();
while(scanRecord.hasNextLine()) {
String line = scanRecord.nextLine();
if (!line.isBlank()) {
...
} else {
donatorList.addDonator(a);
a = new donator();
}