Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

a question to girionis

Posted on 2003-03-12
2
Medium Priority
?
147 Views
Last Modified: 2011-08-18

hi i have a client server application.
in the server side i have two buttons(3 textfileds), and on the client side have just 4 textfileds
to recive data from server.

the problem is that i can't get the data on the other side by pressing the check button(second one)!!!

here is the code from the server program:

public void actionPerformed(ActionEvent e) {
   if(e.getSource() == send){ // send is a button
       ps.println(tf1.getText());
       ps.println(tf2.getText());
       ps.println(tf3.getText());
       }
       if(e.getSource() ==  check){ //check is a button
       ps.println(tf1.getText()+tf2.getText()+tf3.getText());
   }
 }


the first 3 lines sent (when send button pressed) should be displayed in three textfields(result1,result2,result3) and its working well.
the forth line sent (when the check button pressed) should be displayed in the checkResult in the client GUI which is not working.

here is the client codes witch display those lines:

 try{
 s = new Socket("127.0.0.1", 1024);
 br = new BufferedReader(new InputStreamReader(s.getInputStream()));
       ps = new PrintWriter(s.getOutputStream(), true);      
       line = new String(); // Line to be read from the server
       line2 = new String(); // another Line to be read from the server
       line3 = new String();// another Line to be read from the server


// While there is more data coming... // this is working well when send button pressed!
while ( (line = br.readLine()) != null &&
(line2 = br.readLine()) != null &&
(line3 = br.readLine()) != null)
{
    // putting the incomming line from the
    // server into the result fields.
    result1.setText(line);
    result2.setText(line2);
    result3.setText(line3);
}
  line4 = new String(); // here is the problem why it can't display data into checkResult field when the check button pressed?!!
   while ( (line4 = br.readLine()) != null){
checkResult.setText(line4);
}

}

catch(IOException iExc){}

hope its clear.

0
Comment
Question by:Sharabi
  • 2
2 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 75 total points
ID: 8126876
 Are you sure that the value you assign at line4 is not null?

  And a performance tip for you. When you initialize Strings always do it with String line = ""; and not String line = new String(); since the latter for each time you do it it creates a new String and consumes memory, while the former as many times as you do it it assigns the same meory location (so you save memory) :-). So the following:

  String line1 = new String();
  String line2 = new String();
  String line3 = new String();

  will consume 3 different memory locations while the following:

  String line1 = "";
  String line2 = "";
  String line3 = "";

  will only consume one memory location and all three instances of String (lien1, line2, line3) will point to the same.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8127132
 So what was the problem?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question