The entire Chat Application in its glory!

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Client {

	JTextArea incoming;
	JTextField outgoing;
	JButton sendbutton;

	BufferedReader reader;
	PrintWriter writer;

	Socket sock;

	public void go(){
		JFrame frame = new JFrame("Client");
		JPanel mainpanel = new JPanel();

		incoming = new JTextArea(15,50);
		incoming.setLineWrap(true);
		incoming.setWrapStyleWord(true);
		incoming.setEditable(false);

		JScrollPane qScroller = new JScrollPane(incoming);
		qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		outgoing = new JTextField(20);
		sendbutton = new JButton("Send");

		sendbutton.addActionListener(new SendButtonListener());

		mainpanel.add(qScroller);
		mainpanel.add(outgoing);
		mainpanel.add(sendbutton);

		setUpNetworking();

		Thread readerThread = new Thread(new IncomingReader());
		readerThread.start();

		frame.getContentPane().add(BorderLayout.CENTER, mainpanel);
		frame.setSize(400, 400);
		frame.setVisible(true);

	}

	public static void main(String[] args){

		Client client = new Client();
		client.go();
	}

	private void setUpNetworking(){

		try{

			sock = new Socket("127.0.0.1", 5000);
			InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
			reader = new BufferedReader(streamReader);
			writer = new PrintWriter(sock.getOutputStream());
			System.out.println("Networking established!!");

		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public class SendButtonListener implements ActionListener{

		public void actionPerformed(ActionEvent e){
			try{
				writer.println(outgoing.getText());
				writer.flush();
			}catch(Exception ex){
				ex.printStackTrace();
			}

			outgoing.setText("");
			outgoing.requestFocus();
		}
	}

	public class IncomingReader implements Runnable{

		public void run(){
			String message;
			try{
				while((message = reader.readLine()) != null){
					System.out.println("read" + message);
					incoming.append(message + "\n");
					//incoming.setText(message + "\n");
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

}

Advertisements
The entire Chat Application in its glory!

Data Structures using Java Part 1: String objects and I/O in Java

Objects and methods
———————–

String s1; //declare a String variable
s1 = new String(); // assign it a value
String s2 = new String(); // both the steps combined

s1 = "Hello"; // assign the value into the object s1
s2 = s1; // s1 and s2 points to the same object, same value
s2 = new String(s1); // creates a new String object and copies the value of s1 into s2

The 3 String constructors
—————————-
1. new String() constructs a empty string, ie no characters

String s1; //declare a String variable
s1 = new String(); // assign it a value
String s2 = new String(); // both the steps combined

2. Stick something in quotes.

s1 = "Hello"; // assign the value into the object s1

3. Takes a parameter.

s2 = new String(s1); // creates a new String object and copies the value of s1 into s2; the constructor parameter is s1

NOTE: Constructors always have the name of the class

 

String Methods
—————–
1. Conversion into uppercase

s2 = s1.toUpperCase();

2. To concatenate two Strings

String name = s2.concat(" says hello!");
String say = "*".concat(name).concat("*");

Unlike in C, in Java String objects are immutable, ie their contants never change.

 

Java I/O Classes
——————
Objects in System classes for interacting with user

1. System.out
a PrintStream object that outputs to the screen.

2. System.in
a InputStrem object thats reads stuff from the keyboard.

3.readLine method
is defined on a BufferedReader objectm is used to read a whole line.

How do we construct a BufferReader?
With a InputStreamReader.
How do we construct a InputStreamReader?
We need an InputStream.
How do we construct a InputStream?
System.in is one.

The InputStream objects, their job is to read raw data from the keyboard. An unformatted bit stream. The InputStreamReader object takes the raw data and compose them into characters. Java usually uses unicode 2 byte long characters. The ufferedReader object takes the raw characters and converts them into text. This is done in 3 steps for modularity reasons.

import java.io.*;

class SimpleIO{

public static void main( String args[] ) throws Exception{

BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in)
);

System.out.println(keyboard.readLine());
}

}

To use the Java library, other than java.lang, you need to import that library. You need to import the java.io for the ISR and the BR. The java program always begins with the “main”  method.

Data Structures using Java Part 1: String objects and I/O in Java