Friday, October 31, 2014

Java String Pro



Parse CSV File using String To kenizer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
public class ParseCSVFileExample
{
public static void main(String[] args)
{
 try
{
String strFile = "C:/FileIO/example.csv";
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while( (strLine = br.readLine()) != null)
{
lineNumber++;
st = new StringTokenizer(strLine, ",");
                                
while(st.hasMoreTokens())
{
tokenNumber++;
System.out.println("Line # " + lineNumber + ", Token # " + tokenNumber
+ ", Token : "+ st.nextToken());
}
tokenNumber = 0;
}
}
catch(Exception e)
{
System.out.println("Exception while reading csv file: " + e);                  
   }
  }
}

Read Number from Console and Check if it is a Palindrome Number

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;  
public class InputPalindromeNumberExample
{
public static void main(String[] args)
{
System.out.println("Enter the number to check..");
int number = 0;
try
{      
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
number = Integer.parseInt(br.readLine());
}
catch(NumberFormatException ne)
{
System.out.println("Invalid input: " + ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("I/O Error: " + ioe);
System.exit(0);
}
System.out.println("Number is " + number);
int n = number;
int reversedNumber  = 0;
int temp=0;
while(n > 0){
temp = n % 10;
n = n / 10;
reversedNumber = reversedNumber * 10 + temp;
}
if(number == reversedNumber)
System.out.println(number + " is a palindrome number");
else
System.out.println(number + " is not a palindrome number");
   }
}

Java Input Stream to String

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class ConvertInputStreamToStringExample
{   
public static void main(String args[]) throws IOException
{
InputStream is = new FileInputStream("c:/data.txt");
String strContent;
BufferedReader bReader = new BufferedReader(new InputStreamReader(is));
StringBuffer sbfFileContents = new StringBuffer();
String line = null;
while( (line = bReader.readLine()) != null)
{
sbfFileContents.append(line);
}
strContent = sbfFileContents.toString();
strContent = new Scanner(is).useDelimiter("\\A").next();
   }
}

Calculate Rectangle Area using Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalculateRectArea
 {
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 area = length * width;
System.out.println("Area of a rectangle is " + area);
   }
}

No comments:

Post a Comment