Monday, June 21, 2010


Palindrome Number Example

This blog post explains you what is palindrome number and a java example to calculate palindrome number.

Palindrome Number
Palindrome Number is the same number after reversing the actual number.

For Example : 545 (After reversing, the output is same)

The example of palindrom number below :

import java.io.*;

public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a number");
int num= Integer.parseInt(object.readLine());
int n = numb;
int rev=0;
System.out.println("The Number is : ");
System.out.println(" "+ numb);
for (int i=0; i<=num; i++){
int r=numb%10;
numb=numb/10;
rev=rev*10+r;
i=0;
}
System.out.println("Output after reversing: "+ " ");
System.out.println(" "+ rev);
if(n == rev){
System.out.print("This Number is Palindrome!");
}
else{
System.out.println("This Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Unknown Number!");
}
}
}


Saturday, June 12, 2010


Write To File Example

This post will expain you the two classes - FileWriter and BufferedWriter and how to write java program to write to a file.

Class FileWriter - The FileWriter is a class used for writing character files.

Class BufferedWriter - The BufferWriter class is used to write text to a character-output stream, buffering characters. It will help us to provide for the efficient writing of single characters, arrays, and strings.

Find the code below of java program to write text to a file:

import java.io.*;
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}