Sunday 30 March 2014

Java program for character type input using scanner class

import java.io.*;
import java.util.*;
class usr_scanner
{
    public static void main(String args[])
    {
        try
        {
            Scanner sc=new Scanner(System.in);
            System.out.println("Enter any character");
            char s=sc.next().charAt(0);
            System.out.println("You enter character is - "+s);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

Tuesday 18 March 2014

Java Program for Rename File

// rename file

import java.io.File;

class RenameFile
{
    public static void main(String args[])
    {
        File f=new File("c:\\kunal.txt");
        if(f.exists())
        {
            f.renameTo(new File("c:\\amol.txt"));
        }
    }
}

Java Program for Create New File

//wap that will create empty text file


//import java.iostream.*;
import java.io.*;
import java.io.File;
public class NewFile
{
    public static void main(String args[])
    {
        File file=new File("c:\\anant.txt");
        boolean Created=false;
       
        try
        {
            Created=file.createNewFile();
        }
        catch(IOException e)
        {
            System.out.println("error"+e);
            System.out.println("file is created"+file.getPath()+Created);
        }
    }
}



Java Program for LinkedList Demonstration

// wap to insert element and delete element and set another
//element to another position

import java.util.*;
class LinkedListDemo
{
    public static void main(String args[])
    {
        LinkedList l1=new LinkedList();
       
        l1.add(11);
        l1.add("A");
        l1.add("B");
        l1.add("C");
        l1.add("D");
        l1.add("E");
        l1.addLast("z");
        l1.addFirst("A1");
        l1.add(1,"A2");
       
        System.out.println("original content of l1:"+l1);
       
        l1.remove("E");
        l1.remove(2);
           
        System.out.println("content of l1 after deletion :"+l1);
       
        l1.removeFirst();
        l1.removeLast();
       
        //Object val=l1.get(2);
        l1.set(2,"W");
        System.out.println("l1 after change:"+l1);
    }
}


Java Program for Stack Demonstration

//stack demo
import java.util.*;

class StackDemo
{
    static void StackPush(Stack St,int a)
    {
        St.push(new Integer(a));
        System.out.println("push +a");
        System.out.println("stack:"+St);
    }
    static void Showpop(Stack St)
    {
       
        System.out.println("pop:-");
        Integer a=(Integer)St.pop();
        System.out.println(a);
    }
    public static void main(String args[])
    {
        Stack St=new Stack();
        System.out.println("Stack"+St);
        StackPush(St,42);
        StackPush(St,52);
        StackPush(St,62);
        StackPush(St,72);
        Showpop(St);
        Showpop(St);
        Showpop(St);
        Showpop(St);
   
        try
        {
            Showpop(St);
        }
        catch(EmptyStackException e)
        {
            System.out.println("stack underflow"+e);
        }
    }
}










       

Java Program for Delete File

//delete file

import java.io.File;

class DeleteFile
{
    public static void main(String args[])
    {
        File f=new File("c:\\ram.txt");
        if(f.exists())
        {
            f.delete();
        }
    }
}