Choose Your Language

Monday 15 July 2013

java program to read input from console

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {    
     Console console = null;
     try{
         console = System.console();
          if (console != null) {
            String name  = console.readLine("Name: ");          
            char[] pwd = console.readPassword("Password: ");
            System.out.println("Name is: " + name);        
            System.out.println("Password is: "+pwd);
            System.out.println("Password is: "+new String(pwd));
          }    
      }catch(Exception ex){
         ex.printStackTrace();    
      }
   }
}

output:
Name: aravind
Password:
Name is: aravind
Password is: [C@1a758cb
Password is: sankaran

java program to delete duplicate entry in an array

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


public class DeleteDuplicate {

    static int[] a = {5,1,2,3,4,5,7,8,9,10};
    static int[] b = new int[10];
    static int f, i, k, j = 0;
    static int l = a.length;

    static void DeleteElementInt(int elementToDelete) {
        j = 0;
        for (int i = 0; i < l; i++)
            if (a[i] != elementToDelete){
                b[i - j] = a[i];}
            else{
                ++j;
        }
    }
static  int findDuplicate(){
    int num = 0;
    int best = 0;
    for (int i = 0; i < a.length; i++) {
    if (a[i] > best) {
        num = i;
        best = a[i];
    }   return best;
    }
    return best;
}

    public static void main(String[] args) {
       
    System.out.println("Array elements are:");
        for (i = 0; i < a.length; i++)
            System.out.print(a[i]+" ");
        InputStreamReader is = new InputStreamReader(System.in);
       System.out.println("");
       int duplicate=findDuplicate();
       System.out.println("Duplicate entry is "+duplicate);
            DeleteElementInt(duplicate);
            System.out.println("New array:");
            for (i = 0; i < l - j; i++)
                System.out.print(b[i]+" ");
           }
}
   
output:
Array elements are:
5 1 2 3 4 5 7 8 9 10
Duplicate entry is 5
New array:
1 2 3 4 7 8 9 10

Sunday 14 July 2013

java program to find default value of java primitive data types

public class DataTypeDefaultSize {   
     byte  b;
     boolean boo;
     char c;
     short s;
     int i ;
     long l;
     float f;     
     double d;        
     public static void main(String a[]) {
        DataTypeDefaultSize dataTypeDefaultSize=new DataTypeDefaultSize();
        dataTypeDefaultSize.displayDataTypeDefaultSize();
     }
     void displayDataTypeDefaultSize(){
         System.out.println("default value of byte is "+b);
         System.out.println("default value of boolean is "+boo); 
         System.out.println("default value of char is "+c);
         System.out.println("default value of short is "+s);
         System.out.println("default value of int is "+i);
         System.out.println("default value of long is "+l);
         System.out.println("default value of float is "+f);        
         System.out.println("default value of double is "+d);
       
     }
}


output:
default value of byte is 0
default value of boolean is false
default value of char is
default value of short is 0
default value of int is 0
default value of long is 0
default value of float is 0.0
default value of double is 0.0




java program to list all processes that are active in task manager

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

/**
 *
 * @author Aravind Sankaran
 */
public class ListProcessesTaskManager {
    public static void main(String a[]) throws IOException{
       String processes;
       BufferedReader input = new BufferedReader(new  InputStreamReader(Runtime.getRuntime().exec("tasklist.exe").getInputStream()));
       while ((processes = input.readLine()) != null) {
               System.out.println(processes);
                }
        input.close();
    }
}




Thursday 11 July 2013

Java program to find run rate per over

import java.util.Scanner;
public class RunRateCalculator {

    int runs,over;   
    float runRate;
    public void findRunRate(){
         Scanner scanner=new Scanner(System.in);
         try{
            over=1;
            System.out.println("Enter Runs Scored: ");
            runs=scanner.nextInt();
            runRate=runs/over;
            System.out.println(runs+" runs in "+over+" over with a Run Rate of "+runRate+" per over.");   
        }
        catch(NumberFormatException nfe){
            System.out.println(nfe);
            }
        }

    public static void main(String[] abc){
        RunRateCalculator runRateCalculator=new RunRateCalculator ();       
        runRateCalculator.findRunRate();
    }
}

output:
Enter Runs Scored:
10
10 runs in 1 over with a Run Rate of 10.0 per over

How to run java program without main method

public class WithOutMainMethod{
static {
   WithOutMainMethod withOutMainMethod=new WithOutMainMethod();
   System.out.println("inside static block!");
   staticMethod();
   withOutMainMethod.nonStaticMethod();
   System.exit(0);
}
   static  void staticMethod(){
       System.out.println("inside static method!");

   }
   void nonStaticMethod(){
       System.out.println("inside non static method!");

   }
}

output:
inside static block!
inside static method!
inside non static method!

Calculate strike rate of a batsman using java program

public class StrikeRateCalculator {
   
    int ballsFaced, runScored;
    double strikeRate;
    public void findStrikeRate(){
        Scanner scanner=new Scanner(System.in);
         try{
            System.out.println("Enter Runs Scored: ");
            runScored=scanner.nextInt();
            System.out.println("Enter Balls Faced: ");
            ballsFaced=scanner.nextInt();
            strikeRate=(100*runScored)/ballsFaced;
            System.out.println(runScored+" runs scored in "+ballsFaced+" balls, Strike Rate is: "+strikeRate);
        }
        catch(NumberFormatException nfe){
            System.out.println(nfe);           
        }
       
    }

    public static void main(String[] args){
        StrikeRateCalculator strikeRateCalculator=new StrikeRateCalculator();      
        strikeRateCalculator.findStrikeRate();
      
    }
}

Java program to open control panel

import java.io.IOException;

/**
 *
 * @author Aravind Sankaran
 */
public class ControlPanel {
     public static void main(String[] abc){
        try{
             Runtime.getRuntime().exec("control.exe");
            }
        catch(IOException ioe){
            System.err.println(ioe);           
        }
    }
}

Open command prompt using java program

import java.io.IOException;

/**
 *
 * @author Aravind Sankaran
 */
public class OpenCommandPrompt{
     public static void main(String[] abc) {
    try {
          Runtime.getRuntime().exec("cmd /c start ");
         }
    catch (IOException e) {
      System.out.println(e);
        }  
  }
}

Java program to open a notepad

import java.io.IOException;

/**
 *
 * @author Aravind Sankaran
 */
public class OpenNotepad {
     public static void main(String[] abc) {
      try {
            Runtime.getRuntime().exec("notepad");
           }
    catch (IOException e) {
      System.out.println(e);
    }  
  }
}

Java Program to find IP Address of a given site

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 *
 * @author Aravind Sankaran
 */
public class IPAddressFinder {
     public static void main(String args[]) throws Exception
   {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("enter the site address");
       String siteName=br.readLine();
       System.out.println(InetAddress.getByName(siteName));     
   }
}

output:
enter the site address
www.facebook.com
www.facebook.com/31.13.72.33

Java program to print alphabets in capital and small letters

public class DisplayAlphabets {
    public static void main(String abc[])
   {
      char alphabets;

       for( alphabets = 'a' ; alphabets <= 'z' ; alphabets++ )
       {
         System.out.print(alphabets+" ");
       }
       System.out.println("");
       for( alphabets = 'A' ; alphabets <= 'Z' ; alphabets++ )
       {
         System.out.print(alphabets+" ");
       }
   }  
  
}

output:
a b c d e f g h i j k l m n o p q r s t u v w x y z 
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z