Sunday, 15 May 2016

Java code to check whether a number is power of two


import java.util.Scanner;

public class CheckPowerTwo {
   
     
    public static void main(String args[])
    {
    Scanner ob= new Scanner(System.in);
    int a,i;
    System.out.println("Enter an integer");
    a=ob.nextInt();
   
    for( i=0;i<a;i++)
    {
    if(a==Math.pow(2,i))
    { System.out.println("The number is power of two");break;
    }
   
    }
    if(i==a)
        System.out.println("The number is not power of two");
   
   
    }
   
}

Reading a file using FileInputStream in java

import java.io.FileInputStream;

public class ReadFile {
   
   
   
    public static void main(String args[])
        
           
    {  FileInputStream fin ;
    try{
  
    fin= new FileInputStream("C:\\Users\\user\\Desktop\\ash.txt");
    int c;
    while((c=fin.read())!=-1)
    {
       
       
        System.out.print((char)c);
    }
   
    }
    catch(Exception e){}
   
    } 
   
}

Student database GUI using java which adds the records to a text file


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class StudentDatabase extends JFrame{
   static FileOutputStream fou;

  
   
    class Handler   implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
           String s0 = "\r\n";
             byte h0[]=s0.getBytes();
           String s="    ";
           byte h[]=s.getBytes();
            String s1= t1.getText();
            byte h1[]=s1.getBytes();
            String s2= t2.getText();
            byte h2[]=s2.getBytes();
            String s3= t3.getText();
            byte h3[]=s3.getBytes();
            String s4= t4.getText();
            byte h4[]=s4.getBytes();
            String s5= t5.getText();
            byte h5[]=s5.getBytes();

   try{
fou.write(h1);
fou.write(h);
fou.write(h2);
fou.write(h);
fou.write(h3);
fou.write(h);
fou.write(h4);
fou.write(h);
fou.write(h5);
fou.write(h0);
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
JOptionPane j= new JOptionPane();
     j.showMessageDialog(rootPane, "RECORD ADDED");

   }
   catch(Exception ee)
   {}
         
        }
       }
     JTextField t1;
   JTextField t2;
    JTextField t3;
     JTextField t4;
      JTextField t5;
      JButton b;
    StudentDatabase()
    {b= new JButton("ADD");
    Handler k = new Handler();
    b.setBounds(50, 250, 70, 30);
    b.addActionListener(k);
          JLabel j1= new JLabel("STUDENT DATABASE");
    j1.setBounds(90, 30,140, 20);
    t1= new JTextField();
    t1.setBounds(200,50,150,20);
    JLabel j2= new JLabel("STUDENT NAME");
    j2.setBounds(50, 50,150, 20);
    t2= new JTextField();
    t2.setBounds(200,80,150,20);
       JLabel j3= new JLabel("STUDENT ROLL. NO.");
    j3.setBounds(50, 80,150, 20);
     JLabel j4= new JLabel("STUDENT BRANCH");
    j4.setBounds(50, 110,150, 20);
    t3= new JTextField();
    t3.setBounds(200,110,150,20);
     JLabel j5= new JLabel("DOB");
    j5.setBounds(50, 140,150, 20);
    t4= new JTextField();
    t4.setBounds(200,140,150,20);
        JLabel j6= new JLabel("STUDENT SECTION");
    j6.setBounds(50, 170,150, 20);
    t5= new JTextField();
    t5.setBounds(200,170,150,20);
    add(j1);
    add(t1);
    add(j2);
    add(t2);
    add (b);
add(t3);
add(t4);
add(t5);
    add(j3);
       add(j4);
          add(j5);
             add(j6);
            
        setSize(400, 400);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(null);
    setVisible(true);
   
    }
      public static void main(String args[])
         {  new   StudentDatabase();
 
    try{ fou= new FileOutputStream("C:\\Users\\user\\Desktop\\aa.txt");}
    catch(Exception ee)
    {}
   
}
}

-------------------------------------------------------------

import java.io.FileInputStream;

public class ReadFile {
   
   
   
    public static void main(String args[])
        
           
    {  FileInputStream fin ;
    try{
  
    fin= new FileInputStream("C:\\Users\\user\\Desktop\\ash.txt");
    int c;
    while((c=fin.read())!=-1)
    {
       
       
        System.out.print((char)c);
    }
   
    }
    catch(Exception e){}
   
    } 
   
}