Any idea why this doesn't exit when it should socket/serversocket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    Any idea why this doesn't exit when it should socket/serversocket

    Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class SocketConnect
    {
    public static void main(String[] args)
    {
    try
    {
    
    Socket socket = new Socket("192.168.0.6", 4242);
    PrintStream outStream = new PrintStream(socket.getOutputStream());
    Scanner aScanner = new Scanner(System.in);
    String aString = aScanner.nextLine();
    while(aString != "exit")
    {
    
    outStream.println(aString);
    aString = aScanner.nextLine();
    }
    socket.close();
    }
    catch (Exception anException)
    {
    }
    }
    }
    I type exit and it doesn't exit it just shows the word exit on my other computer (serverSocket)

    Regards

    Brendan
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You don't compare Strings for (in)equality like that; use the equals() method for
    that purpose:

    Code:
    while (!aString.equals("exit")) ...
    kind regards,

    Jos

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      doh!

      Probably the first thing i learnt about java.

      Thanks

      Brendan

      Comment

      Working...