title: How to control the printwriter from a separate method?
body: <p>Im working on building my own GUI program that talks to my pc from a tablet. I have the server side done in java but my problem is on the client side. </p>
  
  <p>I want to send data out the PrintWriter to the server from a separate method. 
  I have accomplished sending in the code below (it sends 'a') but i cant figure out how to send it from a separate method. i believe its a basic java scope problem that im not understanding. i would really appreciate the help.</p>
  
  <p>I have tried moving the variables into other scopes.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class TestClient {
  
      public static void main(String[] args) {
          String hostName = "192.168.0.3";
          int portNumber = 6666;
  
          try (   //Connect to server on chosen port.
                  Socket connectedSocket = new Socket(hostName, portNumber);
                  //Create a printWriter so send data to server.
                  PrintWriter dataOut = new PrintWriter(connectedSocket.getOutputStream(), true);
  
                  BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))
              ) {
              //Send data to server.
              dataOut.println("a");
  
          }catch (UnknownHostException e) {
              System.err.println("Don't know about host " + hostName);
              System.exit(1);
          } catch (IOException e) {
              System.err.println("Couldn't get I/O for the connection to " +
                  hostName);
              System.exit(1);
          } 
      }
  
      public static void sendToServer() {
          //I want to control the print writer from this method.
          //I have failed i all the ways i have tried.
  
      }
  }
  </code></pre>