简体   繁体   中英

DataOutputStream condition

i hava an assignment in java and it will accept from user the account number and amount that will added in the balance in database MS accecc and it content client side .. and server side , the server side will connect with database and maching record in database if it found ,the server will returen back the new balance and interst and it work for me but when i use if else condition it not reuten any thing in my case if the data not found reuten 0 from the new balance and interst . the sever side :

while(true){
 cli = myserver.accept();
 DataInputStream ins = new DataInputStream(cli.getInputStream());
 DataOutputStream outs = new DataOutputStream(cli.getOutputStream());
int newbalance;
double  interst;

if(account == fromdb )
{
//here it work and fine .

String name  = rs.getString(3);
 outs.writeUTF(name);
newbalance = (rs.getInt(4))+amount; 
 interst = (newbalance * 0.02);
 outs.writeInt(newbalance);
 outs.flush();
 outs.writeDouble(interst);


}else{
    //here it not work.
    outs.writeInt(0);
 outs.flush();
 outs.writeDouble(0);

}

and client side :

String name  = ins.readUTF();
System.out.println(name+" your detiels: ");
int  b = ins.readInt();
System.out.println("the new balanec is: "+ b);
double  interst = ins.readDouble();
System.out.println("the interst is: "+ interst);

In your working case you have:

outs.writeUTF(name);

In your non-working case, you're missing that.

So you seem to be forgetting to write the name out to the stream in the else case, thus confusing the client side, which is expecting to read a string first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM