Java Tutorial

Program to print alphabets:-

package SeleniumLearn.com;

public class PrintAlphabets{
    public static void main(String[] args){
        
        for(char i='A'; i<='Z'; i++){
            System.out.print(i+" ");

        }    
    }

O/P:-

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

 

Reverse a string in java:-

package SeleniumLearn.Com;

public class Reverse_String {
           
         public static void main(String args[]){
             String original = "moc.nraelmuineles.www";
             
             System.out.println("Original String : " + original);
             
             original = new StringBuffer(original).reverse().toString();
             
             System.out.println("Reversed String : " + original);
             
             System.out.println("========================================");
             
             String originalnum = "9876543210";
             
             System.out.println("Original Number : " + originalnum);
             
             originalnum = new StringBuffer(originalnum).reverse().toString();
             
             System.out.println("Reversed Number : " + originalnum);
             
           }
        }

O/P:-

Original String : moc.nraelmuineles.www
Reversed String : www.seleniumlearn.com

========================================
Original Number : 9876543210
Reversed Number : 0123456789

Even or Odd number in java:-

package SeleniumLearn.Com;

import java.util.Scanner;

public class EvenOrAdd {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter any number to check whether the number is EVEN or ODD ?");
    
    int n = sc.nextInt();
     
     if(n%2==0)
     {
        System.out.println("You entered an Even number : "+n);
     }
        
     else
     {
        System.out.println("You entered an Odd number : "+n);
     }

    }

}

O/P:-

Enter any number to check whether the number is EVEN or ODD ?
99
You entered an Odd number : 99

 

String example in java:-

package SeleniumLearn.Com;

public class StringExample {

             public static void main(String[] args) {
                // Create Strings 
                String k1="Java and Selenium on seleniumlearn.com ";
                String k2= new String ("I Love Selenium and Java");
                char arr[] = {'w','w','w','.','s','e','l','e','n','i','u','m','l','e','a','r','n','.','c','o','m'};
                String k3=new String(arr);
                
                // Display all the 3 Strings
                System.out.println(k1);
                System.out.println(k2);
                System.out.println(k3);
                
                // Find length of k1 string
                System.out.println("Length of k1="+k1.length());
                
                // Adding two k1+k2 strings
                System.out.println("k1 and k2 added="+k1.concat(k2));
                
                //Concatenate k1 string with k3 
                System.out.println(k1+"Visit--> "+k3);
                
                // Test if string k2 start with I
                boolean b=k2.startsWith("I");
                if(b) System.out.println("k2 string starts with \'I\'");
                else System.out.println("k2 string does not start with \'I\'");
                
                //Extract substring from k2, starting from 8th char to 23rd char
                String p =k2.substring(7,24);
                
                //Extract substring from k3, starting from 3rd char to 12th char
                String q =k3.substring(3,12);
                
                System.out.println(p+q);
            
                //convert k3 into upper case to  lower case and upper case to lower case.
                System.out.println("Lower case = "+k3.toLowerCase());
                
                System.out.println("Upper case = "+k3.toUpperCase());
            
                // Check if k1 string equal to k2 string
                if (k1==k2)
                     System.out.println("k1 & k2 strings  are same");
                else System.out.println("k1 & k2 strings  are NOT same");
                
                // Check given number even or odd
                String original = "moc.nraelmuineles.www";
                 
                System.out.println("Original String : " + original);
                 
                original = new StringBuffer(original).reverse().toString();
                 
                System.out.println("Reversed String : " + original);
                    }
            }

 

O/P:-

Java and Selenium on seleniumlearn.com 
I Love Selenium and Java
www.seleniumlearn.com
Length of k1=39
k1 and k2 added=Java and Selenium on seleniumlearn.com I Love Selenium and Java
Java and Selenium on seleniumlearn.com Visit--> www.seleniumlearn.com
k2 string starts with 'I'
Selenium and Java.selenium
Lower case = www.seleniumlearn.com
Upper case = WWW.SELENIUMLEARN.COM
k1 & k2 strings  are NOT same
Original String : moc.nraelmuineles.www
Reversed String : www.seleniumlearn.com

TimeStamp in java:-

 

package SeleniumLearn.Com;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeStamp {

    public static void main(String[] args) {
           DateFormat df = new SimpleDateFormat("dd/MMM/yyyy & HH:mm:ss");
           Date d = new Date();
           String time=df.format(d);
           System.out.println("Display Date and Time : " +time);
    }
}

Output :-
Display Date and Time : 05/Nov/2015 & 08:07:15

 

Read Input from console in Java using Scanner Class

 

package SeleniumLearn.Com;
import java.util.Scanner;
public class ScannerClass {
public static void main(String[] args) {
    Scanner sc= new Scanner (System.in);
            
            System.out.println("Please mention your First Name:");
            String fname=sc.next();
        
            System.out.println("Please mention your Second Name:");
            String sname=sc.next();
            
            System.out.println("Enter Sex(M/F):");
            char sex=sc.next().charAt(0);

            System.out.println("Enter Employee Date of birth:");
            String dob=sc.next();        
            
            System.out.println("Enter a Employee EmailID:");
            String email=sc.next();
            
            System.out.println("Enter Employee ID:");
            int id=sc.nextInt();
            
            System.out.println("Enter your blood group:");
            String blood=sc.next();
            
            System.out.println("Enter your phone number:"); //Visit complete "Java Tutorial" : http://www.seleniumlearn.com/java-tutorial
            double ph=sc.nextDouble();
                    
            System.out.println("Enter Employee Joining date:");
            String jd=sc.next();
            
            System.out.println("Employee Designation:");
            String des=sc.next();
            
            System.out.println("Enter Employee Salary(per annum):");
            float sal=sc.nextFloat();
            
            System.out.println("Enter Office Location:");
            String location=sc.next();
            
            System.out.println("Enter Employee perment address:");
            String address=sc.next();
            System.out.println("=============Employee Details================");
            System.out.println("First Name:"+fname);
            System.out.println("Last Name:"+sname);
            System.out.println("Sex:"+sex);
            System.out.println("Date of birth:"+dob);
            System.out.println("EmailID:"+email);
            System.out.println("Employee ID:"+id);
            System.out.println("Blood Group:"+blood);
            System.out.println("Phone number:"+ph);
            System.out.println("Joining date:"+jd);
            System.out.println("Designation:"+des);
            System.out.println("Salary:"+sal);
            System.out.println("Office Location:"+location);
            System.out.println("Address:"+address);
            }
   }  

Output :-

Please mention your First Name:
Purushotham
Please mention your Second Name:
Karimbedu
Enter Sex(M/F):
M
Enter Employee Date of birth:
21/Oct/1988
Enter a Employee EmailID:
kpurushotham.777@gmail.com
Enter Employee ID:
1099
Enter your blood group:
B+
Enter your phone number:
09000109120
Enter Employee Joining date:
01/Oct/2012
Employee Designation:
QA
Enter Employee Salary(per annum):
320000
Enter Office Location:
Hyderabad
Enter Employee perment address:
ABC
=============Employee Details================
First Name:Purushotham
Last Name:Karimbedu
Sex:M
Date of birth:21/Oct/1988
EmailID:kpurushotham.777@gmail.com
Employee ID:1099
Blood Group:B+
Phone number:9.00010912E9
Joining date:01/Oct/2012
Designation:QA
Salary:320000.0
Office Location:Hyderabad
Address:ABC

 

Comments