Friday, October 31, 2014

Using Java Pro



Calculate Rectangle Perimeter using Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalculateRectPerimeter
{
public static void main(String[] args)
{
int width = 0;
int length = 0;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter length of a rectangle");
length = Integer.parseInt(br.readLine());
System.out.println("Please enter width of a rectangle");
width = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne)
{
System.out.println("Invalid value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
int perimeter = 2 * (length + width);             
System.out.println("Perimeter of a rectangle is " + perimeter);
   }             
}

  Read character from console using Input Stream

import java.io.IOException;
public class ReadCharFromConsoleExample
{
public static void main(String[] args)
{
int iChar = 0;
System.out.println("Read user input character example");
try
{
System.out.println("Enter a character to continue");
iChar = System.in.read();
System.out.println("Char entered was : " + (char)iChar);
}
catch(IOException e)
{
System.out.println("Error while reading : " + e);
     }
   }
}

Java String Buffer append new line

public class JavaStringBufferAppendNewLineExample
{
public static void main(String args[])
{
StringBuffer sbf = new StringBuffer("This is the first line.");
sbf.append(System.getProperty("line.separator"));
sbf.append("This is second line.");
System.out.println(sbf);
    }
}

Calculate Circle Perimeter using Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalculateCirclePerimeterExample
{
public static void main(String[] args)
{
int radius = 0;
System.out.println("Please enter radius of a circle");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
radius = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value" + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}
double perimeter = 2 * Math.PI * radius;
System.out.println("Perimeter of a circle is " + perimeter);
   }
}

Java Pushback Input Stream

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
public class JavaPushbackInputStreamExamples
{
public static void main(String[] args)
{
String strExpression = "a = a++ + b;"; byte bytes[] = strExpression.getBytes();ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
PushbackInputStream pis = new PushbackInputStream(bis);
int ch;
try
{
while( (ch = pis.read())!= -1)
{
if(ch == '+')
{
if( (ch = pis.read()) == '+')
{
System.out.print("Plus Plus");
}
else
{
pis.unread(ch);
                                               
System.out.print("+");
}
}
else
{
System.out.print((char)ch);
      }
   }
}
catch(IOException ioe)
{
System.out.println("Exception while reading" + ioe);
      }
  }
}

No comments:

Post a Comment