0

How to get text from text field and pass into string using BufferedReader object?

I want to get text from text field and want to pass it into string using buffer reader object, and then print that particular text in the text area, and then pass that text using PrintWriter object to socket...

My code..

else if(ae.getSource()==btnsend)
        {
         try   
         {
             PrintWriter pw=new PrintWriter(socket.getOutputStream(), true);
             BufferedReader brk=new BufferedReader(new InputStreamReader(System.in));
             BufferedReader brs=new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String str="";
            while(true)
            {
            // m confused after this..how to proceed..
            jtxts3.getText();
            str=brk.toString();
            jareas1.setText(str);
            pw.println();
            str=brs.toString();
            jareas1.setText("Client Says :-"+str);
            }
        }
          catch(Exception ex)
        {
            System.out.println("Exception caught : "+ex);
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    What is your question ? – Suresh Atta Dec 29 '13 at 10:09
  • 2
    @sᴜʀᴇsʜᴀᴛᴛᴀ Question is buried in title. (I feel the title should not be a question but a statement, with the question in the body. But few would agree with me.) OP For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 29 '13 at 10:23
  • 1
    @AndrewThompson: I strongly agree; the question should (at least) recapitulate the easily overlooked) title. user3143630: For [example](http://stackoverflow.com/a/3245805/230513). – trashgod Dec 29 '13 at 10:37
  • @trashgod *"..the easily overlooked) title"* I'll typically open 9 or ten questions then (when time permits) actually return to each one. At that point, I ignore the titles & get directly into the body of it. The *only* reason I might thereafter notice the title is in searching for '?' in the page (which is a thing I commonly do - mostly because I want to jump direct to the point). ..Continuing my habit of amending posts to add the question into the body. (And prompting people to ensure there is at least one '?' before *"Know someone who can answer?"*.) – Andrew Thompson Dec 29 '13 at 10:53

1 Answers1

1

So I'm going to attempt to break down your question and connect it to the relevant code

Question "I want to get text from text field and want to pass it into string using buffer reader object, and then print that particular text in the text area, and then pass that text using PrintWriter object to socket..."

  • "I want to get text from text field"

Ok that explains this jtxts3.getText();

  • "and want to pass it into string using buffer reader object"

Wait! That makes no sense. A reader just reads. This statement proclaims that you want to pass a string from a text field to a reader object.

  • "and then print that particular text in the text area"

Ok, we can do that. jareas1.setText(str); <-- Your attempt

  • "and then pass that text using PrintWriter object to socket"

We can do that also. pw.println(); <-- your attempt

Sorry I needed to do all that to try and understand your flow. But one thing I don't understand about your code is that you have a BufferedReader that you are attempting to get text from the console and also a text field you are trying to get text from. Which one is it?


I'm going to pretend the BufferedReader System.in object does not exist, since I see no context for it. With all this said. It seems like the flow of your code is somewhat correct. The only faults I can spot are

  1. You are setting the text to the text area instead of appending. What setting the text does is overwrite the previous text. So you probably want to use append instead of setText
  2. You're using println but not passing anything to it. maybe you should actually pass the string.
  3. What exactly do you think this does brs.toString();. Do you even know how to read get input from a BufferedReader? Here a tutorial How to Read from and Write to Sockets.

Sorry, I attempted to answer this question, but as I neared a possible solution, My mind boggled even more every time I looked at your code. So take a look at the tutorial. Hope it helps. And next time try to word your question more carefully in explaining the logical order of what you're trying to accomplish as well as how it fits in with your coding attempt.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720