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!");
}
}
}
Monday, June 21, 2010
Saturday, June 12, 2010
Write To File Example
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());
}
}
}
Thursday, November 26, 2009
Example to Determine if a File or Directory Exists
import java.io.*;
public class filedirexists{
public static void main(String args[])
{
File filedir=new File("File or Directory exists or not!!")
boolean exists = filedir.exists()
if (!exists)
{
System.out.println("The File/Directory you are looking for is not found")
}
else
{
System.out.println("The File/Directory you are looking for available")
Sunday, November 15, 2009
Comparing Two Dates
The compareTo() method returns the following integer value if comparing two dates :
The integer value '0' returns if both dates are equal.
The integer value '1' returns when first date is greater than second date.
The integer value '-1' returns when first date is less than second date.
Here is the example of comparing 2 dates and display the final result :
import java.util.Date;
public class Comparedates{
public static void main(String[] args) {
Date Date1 = new Date();
try{
Thread.sleep(1000);
}catch(Exception e){
}
Date Date2 = new Date();
System.out.println("First Date:="+Date1);
System.out.println("Second Date:="+Date2);
if(Date1.compareTo(Date2) > 0)
System.out.println("Date1 is Greater than Date2");
else if(Date1.compareTo(Date2) < 0)
System.out.println("Date1 is less than Date2");
else
System.out.println("Date1 and Date2 are equal!!!");
}
}
Wednesday, March 25, 2009
JDBC and JDBC Driver Connectivity
JDBC is an application program to the core used to access database in tubular format encrypted by Java and driven by standard interfaces. RDBMS like SQL and Oracle are quarried and updated by JDBC with its niche in using and allowing heterogeneous implementation with the same application.
Since SQL is supported by all Relational Database Management System any corporate database could be accessed, quarried or updated via this single Java written JDBC program. In order to interact with database Java interface is used by programmers but it is the job of a JDBC driver to implement the interface according to the data base management system. Though Java Database Connectivity Driver categorically implements connection some are better suited for specific applications over the other types.
Four types of JDBC driver connectivity are common with Type 1 driver heading the list. It’s a popular, platform independent driver that uses ODBC to connect to database. Type 2 Native API driver is not coded by Java in its entirety making it more versatile in interpreting client libraries of database into recognizable native calls.
Application server, namely middle tier technology is used by Type 3 drivers and therefore client software is not required and database access could be gained through internet.
A type 4 driver also known as Native Protocol Driver and is a complete Java written program, which displays superior performance over the other two types and is completely platform independent. Communication with client application is speedy and easy as direct conversion of JDBC into vendor database protocol is enabled excluding middleware function; however a separate client side driver is required for a different database which poses the only challenge at times.
Labels:
java blog,
jdbc,
jdbc driver connectivity
Tuesday, March 17, 2009
Changing Mouse Icon Example
import java.awt.*;
import java.awt.event.*;
public class ChangeCursor{
public static void main(String[] args) {
Frame m = new Frame("Change cursor");
Panel panel = new Panel();
Button b1 = new Button("Yes");
Button b2 = new Button("No");
panel.add(b1);
panel.add(b2);
m.add(panel,BorderLayout.CENTER);
m.setSize(300,300);
m.setVisible(true);
Cursor c1 = b1.getCursor();
Cursor c2 = b2.getCursor();
b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
b2.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
m.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
}
Labels:
awt example,
java beginners example,
mouse icon
Wednesday, December 31, 2008
Disable Keyboard Editing in JSpinner
import javax.swing.*;
import java.awt.*;
public class Disablekeyboard{
public static void main(String[] args){
JFrame frm = new JFrame("Disable Keyboard Editing Example");
JSpinner spinner = new JSpinner();
JFormattedTextField tf = ((JSpinner.DefaultEditor)spinner.getEditor())
.getTextField();
tf.setEditable(false);
spinner.setValue(new Integer(100));
JPanel panel = new JPanel();
panel.add(spinner);
frm.add(panel, BorderLayout.NORTH);
frm.setSize(350, 350);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Labels:
jspinner example,
jspinner swing example
Thursday, October 16, 2008
Java sort() Method Example
import java.util.*;
import java.io.*;
import java.lang.String;
public class sortstr{
public static void main(String args[]){
String str = "Java String Example";
char[] content = str.toCharArray();
java.util.Arrays.sort(content);
String sorted = new String(content);
System.out.println(content);
}
}
Result : EJSaaegilmnprtvx
Labels:
java sort(),
java string sort(),
sort() method
Friday, September 26, 2008
Show Icon on The Button
import javax.swing.*;
import java.awt.*;
public class IconButton{
public static void main(String[] args){
JFrame frame = new JFrame("Swing Icon Example");
JButton button = new JButton("My Button");
Icon imgicon = new ImageIcon("smiley_icon.gif");
JPanel panel = new JPanel();
button.setIcon(imgicon);
panel.add(button);
frame.add(panel, BorderLayout.NORTH);
frame.setSize(380, 380);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Saturday, September 13, 2008
Swing Input Dialog Box
import javax.swing.*;
import java.awt.event.*;
public class ShowInputDialog{
public static void main(String[] args){
JFrame frame = new JFrame("Input Dialog Box Example");
JButton button = new JButton("Click for Input Dialog Box");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String txt = JOptionPane.showInputDialog(null, "Enter Your Text : ",
"Input Box", 1);
if(txt != null)
JOptionPane.showMessageDialog(null, "The text you have entered : " + txt,
"Input Box", 1);
else
JOptionPane.showMessageDialog(null, "You clicked on cancel button.",
"Input Box", 1);
}
});
JPanel panel = new JPanel();
panel.add(button);
frame.add(panel);
frame.setSize(350, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Labels:
java input box,
swing input box example
Tuesday, September 2, 2008
Comparing Two Strings Using equals()
import java.lang.*;
import java.io.*;
public class equalstrings{
public static void main(String[] args) throws IOException{
System.out.println("String Compares Example Using Equals() Method");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter First String:");
String str1 = bf.readLine();
System.out.println("Enter Second String to Compare :");
String str2 = bf.readLine();
if (str1.equals(str2)){
System.out.println("Both the Strings are Equal!");
}
else{
System.out.println("Both the Strings are not Equal!");
}
}
}
Saturday, August 23, 2008
Java String Length Example
import java.lang.*;
import java.io.*;
public class Strlen{
public static void main(String[] args) throws IOException{
System.out.println("Count the Lenght of String");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter String:");
String str = bf.readLine();
int len = str.length();
System.out.println("Length of Text : " + len);
}
}
Tuesday, July 1, 2008
Java Prime Number Example
Definition : A prime number is a natural number that has only two divisor. Either it is divisible by 1 or itself and it is greater than 1. So we can say it has only one divisor means it can only divided by itself.
Here is the example of prime number in Java. It display all the prime numbers between 1 and the limit entered by user. Get the source code of prime number example here :
import java.io.*;
class Primenumberexample {
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter Limit to Print Prime Number :");
int k = Integer.parseInt(bf.readLine());
System.out.println("Prime Numbers Are : ");
for (int i=1; i < k; i++ ){
for (int j=2; j < i; j++){
int a = i%j;
if (a==0){
break;
}
}
if(i == j){
System.out.print(" "+i);
}
}
}
}
Monday, June 30, 2008
How to Calculate Area of Rectangle?
class Rectangleexample
{
public static void main(String[] args)
{
int len=5, breadth=7, area=1;
area = len*breadth;
System.out.println("Area if Rectangle :" + area);
}
}
Saturday, June 21, 2008
Count Vowels Example in Java
import java.lang.String;
import java.io.*;
import java.util.*;
public class vowels{
public static void main(String args[])throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter New String to Check :");
String text = bf.readLine();
int cnt = 0;
for (int a = 0; a < text.length(); a++) {
char b = text.charAt(a);
if (b=='a' || b=='e' || b=='i' || b=='o' || b=='u') {
cnt++;
}
}
System.out.println("Total Vowels are" + " : " + cnt);
}
}
Labels:
java example,
java source code,
vowel count example
Tuesday, June 17, 2008
How to Create Checkbox in Java Swing?
A check box or tick box is a graphical user interface element that allow or permits user to make selections from the options. User can select more than one item or element.
The given example will explain you how to create check box in java swing. It is created in java by creating the instance of JCheckBo class. Here is the check box program :
public class CreateCheckBox{
public static void main(String[] args){
JFrame frame = new JFrame("The Check Box Example");
JCheckBox chk = new JCheckBox("Check Box 1");
JCheckBox chk = new JCheckBox("Check Box 2");
JCheckBox chk = new JCheckBox("Check Box 3");
frame.add(chk);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Labels:
beginners java,
java swing,
jcheckbox,
swing example
Tuesday, May 27, 2008
Java String Trim Example
Here is the example how to remove blank spaces :
import java.lang.*;
public class StringTrim{
public static void main(String[] args) {
System.out.println("Source code of String Trim Method");
String str = " blankspaces ";
System.out.println("Entered String :" + str);
System.out.println("Output of the Program :" +str.trim());
}
}
Friday, April 18, 2008
Display Time in JSpinner : Swing Examples
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ShowTimeJSpinner{
public static void main(String[] args) {
ShowTimeJSpinner h = new ShowTimeJSpinner();
}
public ShowTimeJSpinner(){
JFrame frame = new JFrame("JSpinner Time Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Date date = new Date();
SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
JSpinner spinner = new JSpinner(sm);
JSpinner.DateEditor de = new JSpinner.DateEditor(spinner, "hh:mm");
spinner.setEditor(de);
frame.add(spinner,BorderLayout.NORTH);
frame.setSize(100,100);
frame.setVisible(true);
}
}
Saturday, March 29, 2008
How to Create Combo Box : Java Swing Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ComboBox{
JComboBox combo;
JTextField txt;
public static void main(String[] args) {
ComboBox b = new ComboBox();
}
public ComboBox(){
String course[] = {"India","Germany","America","Russia"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
txt = new JTextField(10);
panel.add(combo);
panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie){
String str = (String)combo.getSelectedItem();
txt.setText(str);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
Thursday, March 20, 2008
Maximize a Frame using Swing
import java.awt.*;
import javax.swing.*;
public class SwingSetBounds{
public static void main(String[] args){
JFrame frame = new JFrame();
Rectangle bounds = new Rectangle(0, 0, 500, 500);
frame.setMaximizedBounds(bounds);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Subscribe to:
Posts (Atom)