Tags » Inputstreamreader

write a program to reverse string in java

program to reverse string  :


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ComparableExample {

/**
* @auther pradeep
*/
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter String to reverse :");
String str = br.readLine();
String rev = "";
int n = str.length();
for (int i = n - 1; i >= 0; i--) {
rev = rev + str.charAt(i);

}
System.out.print("String after reverse are :" + rev);
}

catch (Exception e) {
System.out.println("" + e.getMessage());
}
}

}

… 11 more words
Java

print triangle shape

Print triangle :


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author pradeep
*/
public class TriangleShape {

public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a number :");
int n = Integer.parseInt(br.readLine());
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output :

Enter a number :
4
*
***
*****
*******
*********

Java

Array through user input in java

Array through user input : 

A user can enter an array length which you can see in below code.


package pkg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author pradeep
*/
public class Example {

public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter lengh of array :");
int n = Integer.parseInt(br.readLine());
for (int i = 0; i <= n; i++) {
System.out.print(" " + i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

… 6 more words
Java

>Program for employee information.

>import java.io.*;
class Person
{
String name;
int age;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
void accept()
{
try
{
System.out.print(“Enter the Name : “); 100 more words

Java

>Program to average Sale of the Week.

>import java.io.*;
class Exp7_1
{
public static void main(String[] args)
{
float sale[] = new float[7];
float avg,sum=0;
try
{
InputStreamReader isr = new InputStreamReader(System.in); 52 more words

Java

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();
			}
		}
	}

}

Java

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();
			}
		}
	}

}

Java