Sunday, March 31, 2013

Cryptography with Java : Program Source Codes

5. For, While and Do While Loops in Java :


import java.io.*;
import java.util.*;
/**
 *
 * @author pranshu
 */
public class Loops_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Hello, Welcome to Pranshu's loops...");
        System.out.println("\nHow many times do you want to loop??  ");
        Scanner scn = new Scanner(System.in);
        int times = scn.nextInt();
        System.out.println("You Entered " + times);
        for(int i = 1; i <= times ; i++)
            System.out.println("Inside For Loop, The no. is " + i );
        int i = 1;
        do
        {
            System.out.println("Inside Do-While Loop, The no. is " + i );
            i++;
        } while(i <= times);
        
        i = 1;
        while( i <= times)
        {
            System.out.println("Inside While Loop, The no. is " + i );
            i++;
        }
    }
    
    
}


6. Conversion From Decimal to Binary in Java, Source code:



/**
 *
 * @author root
 */
public class conversions_pranshu {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter a Decimal : ");
       BufferedReader brobj = new BufferedReader (new InputStreamReader(System.in));
        String dec = brobj.readLine();
        int dec2 = Integer.parseInt(dec);
        String bin = Integer.toBinaryString(dec2);
        System.out.println("The Binary is : " + bin);
    }
   
} 
 
 

7. Search for a Word in Text or String, Java Source code :

 
 
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
/**
 *
 * @author pranshu
 */
public class matchtext {
    
   
    
    public static void main(String[] args)
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Text: ");
    try
    {
    String text = br.readLine();
    System.out.println("Enter the word to match : ");
    String word = br.readLine();
    String[] strarr = text.split(" ");
    int flag =0;
    for(String str:strarr)
    {
    if(str.equalsIgnoreCase(word))
    {
        System.out.println(word + " exists in " + text);    
        flag = 1;
    }
    }
    if(flag == 0)
        System.out.println("No match found");
    
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    } 
 
 

8. File Handling Program in Java : Write to a File: 

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
 *
 * @author pranshu
 */
public class filehandle_pranshu {
    
    public static void main(String[] args) throws IOException
    {
          String com = "comment";
          String name = "uname";
 
          String ipadd = "127.0.0.1" ;

          FileWriter fstream = new FileWriter("/root/comments.txt",true);
          BufferedWriter fbw = new BufferedWriter(fstream);
            
            fbw.write(name + "__" + ipadd + ": ");
            fbw.write(com);
            fbw.newLine();
            System.out.println("Done Writing...");
            System.out.println("Closing File now...");
            fbw.close();
        
    }
    
}

No comments:

Post a Comment