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-08
3
Medium Priority
?
194 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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

571 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