?
Solved

a question to girionis

Posted on 2003-03-08
3
Medium Priority
?
191 Views
Last Modified: 2010-03-31
hi again...
in my previous question http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20542500.html
you have answered my quwstion:

>>3.how many PrintWriters do we need if we want to send Strings from about ... say 5 textfileds?

 >>Again one. You can use the same PrinWriter instance to write to the text fields, no matter how many text fields you have.

i still can't get it right!
for example:
for the bufferedReader i can do it using a single bufferedReader as follows:
   
   String line = null;
   String line2 = null;
   line = new String(); // Line to be read from the server
   line2 = new String();
   / While there is more data coming...
while ( (line = br.readLine()) != null &&
(line2 = br.readLine()) != null)
{
    result1.setText(line);
    result2.setText(line2);
}

but for the PrintWriter i dont know how to do it!!!
i've tried something like this:

     try{
 s = new Socket("127.0.0.1", 1024);
 br = new BufferedReader(new InputStreamReader(s.getInputStream()));

 //true ensures autoflushing
       ps = new PrintWriter(s.getOutputStream(), true);  
       }
       catch(IOException ex){
       }
       

   public void actionPerformed(ActionEvent e) {
       String print, print2;
       print1 = new String; // i want to make them instace of the PrintWriter!
       print2 = new String; //how can it be done?!

       if(e.getSource() == send){

       print.println(tf1.getText()); // not sure about it!!
       print2.println(tf2.getText());

       }

   }
i appritiate your help.
0
Comment
Question by:CJEH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 8094518
 You only need to obtain the PrintWriter object once. You obtain it here:

   ps = new PrintWriter(s.getOutputStream(), true);

  and you can use it over and over in your programme. Your actionPerformed method:

public void actionPerformed(ActionEvent e) {
      String print, print2;
      print1 = new String; // i want to make them instace of the PrintWriter!
      print2 = new String; //how can it be done?!
      if(e.getSource() == send){
      print.println(tf1.getText()); // not sure about it!!
      print2.println(tf2.getText());
      }
  }

  is complicated. You do not need the two String "print" and "print2" since you can use the "ps" instance straight away.

  It should be:

public void actionPerformed(ActionEvent e)
{
     if(e.getSource() == send)
         {
          ps.println(tf1.getText());
              ps.println(tf2.getText());
     }
}

  where you can see that you use the "ps" instance to write the text.

> print1 = new String; // i want to make them instace of the PrintWriter!

  You cannot. You cannot convert String to PrintWriter. You already have the "ps" instance of the PrintWriter object so use this one :-)
0
 

Author Comment

by:CJEH
ID: 8095569
ahaaa ... :)
its clear now
thanks for your help.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8095656
 Thank you for the points :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 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