帮我一下

justcurry 2015-07-20 06:39:06
我的类
public class Account {
private int deposit;//存款
private int sno;//账户号码
private int smm;//账号密码
private String sname;//账户名
public Account(){}
public Account(int x,int y,int z,String g){
deposit=x;
sno=y;
smm=z;
sname=g;
}
public int getDeposit() {
return deposit;
}
public void setDeposit(int deposit) {
this.deposit = deposit;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public int getSmm() {
return smm;
}
public void setSmm(int smm) {
this.smm = smm;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String tostring(){
return ("名字为:"+sname+"开了账户号为:"+sno+"密码为:"+smm+"存款余额为:"+deposit);
}
}
存入文件什么的
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Accountrun{
private Account a;
public Account geta() {
return a;
}
public void seta(Account a) {
this.a = a;
}
public Accountrun(){}
public Accountrun(Account x){
a=x;
}
public void create(Account a) throws IOException{
FileWriter f=new FileWriter("G://java/daima/717/aa.txt",true);
f.write(a.toString()+"\r\n");
f.flush();
f.close();
}
public Account[] quchu() throws IOException{
FileReader f=new FileReader("G://java/daima/717/aa.txt");
BufferedReader bre=new BufferedReader(f);
Account[] a=new Account[100];
String str="";
try {
for(int i=0;(str=bre.readLine())!=null;i++) //判断最后一行不存在,为空
{
//这边我的理解是匹配“:”和“;”中间的字符串(算是正则??)
Pattern p=Pattern.compile(":(\\w+);");
//建设实例
Matcher m=p.matcher(str);
a[i].setSname(m.group(1));
a[i].setSno(Integer.parseInt(m.group(1)));
a[i].setSmm(Integer.parseInt(m.group(1)));
a[i].setDeposit(Integer.parseInt(m.group(1)));
//这两个是区别带不带之前的边界
//System.out.println(m.group(0));
//System.out.println(m.group(1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bre.close();
return a;
}
public static void main(String[] args) throws IOException
{
Accountrun r=new Accountrun();
Account[] a=r.quchu();
for(int i=0;i<a.length;i++)
{
System.out.println(a[i].getSno()+a[i].getSname());
}

}

}
报错
Exception in thread "main" java.lang.NullPointerException
at Accountrun.quchu(Accountrun.java:38)
at Accountrun.main(Accountrun.java:59)
...全文
85 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
oh_Maxy 版主 2015-07-21
  • 打赏
  • 举报
回复
Matcher m=p.matcher(str); 在这之后,要m.find()判断下,不能直接m.group(1) 而且m.group(1)用了好几个地方,还会转换成Integer,但是\\w+匹配的是字符串,有可能有字母的。

50,350

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧