Topic: 谁能帮我看看这个类,为什么read()方法没有用啊?!!

  Print this page

1.谁能帮我看看这个类,为什么read()方法没有用啊?!! Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 09:24

/*以下是储户信息类OneDeposit,用来保存储户信息以及对储户信息进行
一系列的处理如挂失,取款等*/
import java.io.*;
class OneDeposit{
String number,name,password,capital,sort,saveDay,address,saveAssistant;
boolean isLost;
OneDeposit(){}
OneDeposit(String number,String name,String password,String capital,
String sort,String saveDay,String address,String saveAssistant,boolean b){
this.number=number;
this.name=name;
this.password=password;
this.capital=capital;
this.sort=sort;
this.saveDay=saveDay;
this.address=address;
this.saveAssistant=saveAssistant;
isLost=b;
}
void createFiles()throws IOException{
File f=new File("存款文件.dat");
if(!f.exists()){
RandomAccessFile out=new RandomAccessFile("存款文件.dat","rw");
}
}
void write(RandomAccessFile out)throws IOException{
out.writeUTF(number);
out.writeUTF(name);
out.writeUTF(password);
out.writeUTF(capital);
out.writeUTF(sort);
out.writeUTF(saveDay);
out.writeUTF(address);
out.writeUTF(saveAssistant);
String str;
if(isLost){str="true";}
else{str="false";
}
out.writeUTF(str);
}
//读取一个账号的相应储户信息,返回它的对象
OneDeposit read(RandomAccessFile in,String saveNumber)throws IOException{
String str,strr;
String str1,str2,str3,str4,str5,str6,str7,str8;
try{
while((str=in.readUTF())!=null){
System.out.println(str+saveNumber);
if(str==saveNumber){
  str1=str;
  str2=in.readUTF();str3=in.readUTF();
  str4=in.readUTF();str5=in.readUTF();
  str6=in.readUTF();str7=in.readUTF();
  str8=in.readUTF();
  strr=in.readUTF();
System.out.println(strr);
if(strr=="true") {isLost=true;}
else {isLost=false;}

OneDeposit aaa=new OneDeposit(str1,str2,str3,str4,str5,str6,str7,str8,isLost);
return aaa;
}
   for(int i=0;i<=7;i++){String str9=in.readUTF();}
}
}catch(EOFException e){}
return new OneDeposit();
}
}

这个类的read()方法是为了查找一个跟saveNumber一致的储户的信息,但是却读不出来整个信息,只有isLost被赋了值。有哪位大侠能帮我改一下啊,好急啊!!!!多谢多谢了!
我的QQ:241866421
再次谢谢!

2.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: why
Posted on: 2004-09-06 09:41

if(str==saveNumber)
if (strr=="true")
should be
if (str.equals(saveNumber))
if (strr.equals("true"))

check other String comparisons...
and there could be other problems, not to mention the implementation, style, etc.

3.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 10:27

没有用啦,我试着输出几个成员变量的值,但是都是null
急啊~~~~~~~~~~~~~~~~~`

4.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: why
Posted on: 2004-09-06 11:03

虽然没有用,但这是明显的错误。

用 debugger 找不到错吗?
还是未用呢?

敝人没用过 RandomAccessFile,但 Random Access 不是要处理 file pointer 吗?看不见。

5.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 11:10

没有啊
我还是初学者啊,那些错误都改了可还是没用
不知道哪位大哥能帮忙修改一下

6.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: why
Posted on: 2004-09-06 11:13

Alerander wrote:
没有啊
我还是初学者啊,那些错误都改了可还是没用
不知道哪位大哥能帮忙修改一下

怎样修改?
多半要重写,哪岂非替阁下做家课?Sad

>> 敝人没用过 RandomAccessFile,但 Random Access 不是要处理 file pointer 吗?看不见。
到底有没有处理?
看来须要加上 in.seek(0); 从头开始读的。

阁下说“试着输出几个成员变量的值,但是都是null”
到底怎样试?

阁下还是先把调用这个 OneDeposit 类的咚咚都贴上来吧。

7.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 11:25

这位大哥言重了,这是一个基础类,没有它根本谈不上调用它的类啊!这样吧,我把它的需求帖过来吧:

1.“储户信息”类
属性
账号,姓名,密码,地址,储种,本金,收储日期,是否已挂失及挂失日期,以及存款时营业员信息
服务
创建
接口:无返回值,参数为:姓名,密码,地址,储种,本金,收储 日期,是否已挂失及挂失日期,以及存款时营业员信息
功能:用给定的参数创建一个储户信息对象
创建存款文件
接口:无返回值,无参数
功能:检查是否存在存款文件,若不存在则创建之
读取存款信息
接口:参数为输入流对象“存款文件.dat”和String对象saveNumber
功能:从输入流读取账号为saveNumber的储户信息对象
写入储户信息
接口:无返回值,参数为输出流对象“存款文件.dat”
功能:将一个储户信息写到输出流中
删除储户信息
接口:无返回值,参数无
功能:删去该储户信息对象

表述上可能有点问题还请见谅!这个问题问了老师也解决不了,不得已烦劳各位高手,在下再次谢谢各位了!

8.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 11:26

对了~~
如果改用成BufferedWriter和BufferedReader也可以的~~~~

9.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: why
Posted on: 2004-09-06 11:29

Alerander wrote:
这是一个基础类,没有它根本谈不上调用它的类啊!

晕!Dead
那么“输出几个成员变量的值,但是都是null” 这句话从何说起?


如果改用成BufferedWriter和BufferedReader也可以的~~~~

哪裡改?
怎样改?
敝人不是阁下肚裡的迴虫,很难猜度得出阁下的想法的说。

10.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 11:31

import java.io.*;
class OneDeposit{
String[] info=new String[9]; //声明存储储户信息的数组
OneDeposit(){}
public OneDeposit(String number,String name,String password,String capital,
String sort,String saveDay,String address,String saveAssistant,String isLost){
info[0]=number; info[1]=name; info[2]=password;
info[3]=capital; info[4]=sort; info[5]=saveDay;
info[6]=address; info[7]=saveAssistant; info[8]=isLost;
}
void createFiles()throws IOException{
File f=new File("存款文件.dat");
if(!f.exists()){
BufferedWriter out=new BufferedWriter(new FileWriter("存款文件.dat"));
out.close();
}
}
void write(BufferedWriter out)throws IOException{
for(int i=0;i<9;i++){
out.write(this.info[i]);
out.newLine();
}
}
int search(BufferedReader in,String number)throws IOException{
int line=0,i;
while(in.readLine()!=null){line++;}
String[][] info2=new String[line/9][9];
for(int a=0;a<info2.length;a++){
for(int b=0;b<info2[a].length;b++){
info2[a]=in.readLine();
}
}
//测试区
for(int c=0;c<info2.length;c++){
  for(int d=0;d<info2[c].length;d++){System.out.println(info2[c][d]);}
}
for(i=0;i<info2.length;i++){
if(info2[i].equals(number)) {
for(int j=0;j<9;j++){info[j]=info2[i][j];}
return 1;
}
}
return -1;
}
}
public class TestOneDeposit{
public static void main(String[] args){
OneDeposit d1=new OneDeposit();
d1.info[0]="19860923";
d1.info[1]="李四";
d1.info[2]="abcdef";d1.info[3]="65300";d1.info[4]="三年";
d1.info[5]="20041001";d1.info[6]="中华人民共和国";
d1.info[7]="混蛋007";d1.info[8]="false";
for(int j=0;j<9;j++){System.out.println(d1.info[j]+" ");}
try{
BufferedWriter out=new BufferedWriter(new FileWriter("存款文件.dat",true));
d1.write(out);
}catch(IOException el){System.out.println(el);}
try{
BufferedReader in=new BufferedReader(new FileReader("存款文件.dat"));
OneDeposit d2=new OneDeposit();
int k=d2.search(in,"19860923");
for(int j=0;j<9;j++){System.out.println(d2.info[j]+" ");}
}catch(IOException e){}
}
}


又试着写了一个这样的程序,还是没用啊~~~~急噪咯!

11.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 11:34

import java.io.*;
class TestOneDeposit{
public static void main(String[] args)throws IOException{
boolean b=false;
OneDeposit od=new OneDeposit("19860923","liye","fafadf","fafasf","fafafda",
"fafdfa","wew","wee",b);
OneDeposit odd=new OneDeposit("19660923","yydhe","fafadf","fafasf","fafafda",
"fafdfa","wew","wee",b);
od.createFiles();
RandomAccessFile out=new RandomAccessFile("存款文件.dat","rw");
od.write(out);
odd.write(out);
}
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

import java.io.*;
class TestOneDeposit2{
public static void main(String[] args)throws IOException{
boolean b=false;
OneDeposit od=new OneDeposit();
RandomAccessFile in=new RandomAccessFile("存款文件.dat","rw");
od.read(in,"19860923");

System.out.println(od.number+od.name+od.password+od.capital+od.sort+od.saveDay+od.address+od.saveAssistant+od.isLost);
}
}

》》》》》》》》》》》》》》》

这就是刚刚的测试程序,很简单的

12.Re:谁能帮我看看这个类,为什么read()方法没有用啊?!! [Re: Alerander] Copy to clipboard
Posted by: Alerander
Posted on: 2004-09-06 11:41

这位大哥,就是修改了能让它读出想要的帐户信息啊!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923