I am trying to learn java. I want to know whats the best way of doing it?
For example I got the code for setting up a TCP server in java which sends the data given in string. Now I want it to send the data dynamically to the client as the user types it.eg a sentence.How to do it? Here are the client and server codes.

Server.java

import java.lang.*;
import java.io.*;
import java.net.*;

class Server {
public static void main(String args[]) {
String data = "Toobie ornaught toobie";
try {
ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}

Client.java

import java.lang.*;
import java.io.*;
import java.net.*;

class Client {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");

while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it

System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}

Please help me learn java..
thank you.

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

IMHO copy/pasting code is not a good way to start to learn Java. Even if you read it all, you will not understand why it was written that way.
I recommend you start with an empty text editor and build the "hello world" app from scratch, then move on to bigger and better things. Save copy/paste until you have a solid understanding of Java basics and are just looking for very specific solutions to specific problems.
If you are serious about learning Java properly the official Java Tutorials are very good indeed. Highly recommended.
http://download.oracle.com/javase/tutorial/index.html

IMHO copy/pasting code is not a good way to start to learn Java. Even if you read it all, you will not understand why it was written that way.
I recommend you start with an empty text editor and build the "hello world" app from scratch, then move on to bigger and better things. Save copy/paste until you have a solid understanding of Java basics and are just looking for very specific solutions to specific problems.
If you are serious about learning Java properly the official Java Tutorials are very good indeed. Highly recommended.
http://download.oracle.com/javase/tutorial/index.html

Thanks

Formal education. Concepts like algorithm efficiency and design patterns do not appear in the self-taught curriculum, from my experience. Bad habits, once engraved, can be difficult to overcome -- don't set yourself up for failure.

Disregarding that -- I agree with James, the Oracle tutorials are excellent.

This article has been dead for over six months. Start a new discussion instead.