Skip to main content

Java Program to show the use of some String Library classes

import java.io.*;
import java.util.*;
public class StringLibrary{
    Scanner scan=new Scanner(System.in);
    String first, last, replace, replaceWith, compare, compareWith, indexOf, number;
    int index;
    char character;
    public void input(){
        try{
            System.out.println("Enter your First Name: ");
            first=scan.nextLine();
            System.out.println("Enter your Last Name: ");
            last=scan.nextLine();
        }
        catch(Exception e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
    }

    public void compute(){
        number=first.concat(" "+last);
        lengthString();
        characterIndex();
        replacing();
        compare();
        indexOf();
        caseConversion();
    }

    public void lengthString(){
        int length=first.length()+last.length();
        System.out.println("Length of String including blank spaces: "+length);
    }

    public void characterIndex(){
        try{
            System.out.println("Enter index number: ");
            index=scan.nextInt();
        }
        catch(Exception e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
        System.out.println("Character at Index \""+index+"\" is: "+(number.charAt(index)));
    }

    public void replacing(){
        try{
            System.out.println("Enter any String you want to replace: ");
            replace=scan.next();
            System.out.println("Enter the String you want to replace with: ");
            replaceWith=scan.next();
        }
        catch(Exception e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
        System.out.println("After replacing: "+(number.replace(replace, replaceWith)));
    }

    public void compare(){
        try{
            System.out.println("Enter the String you would like to compare: ");
            compare=scan.next();
            System.out.println("Enter the String you want to compare it with: ");
            compareWith=scan.next();
        }
        catch(Exception e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
        System.out.println("Result in True/False:- \nString comparison: "+(compare.equals(compareWith)));
        if(compare.compareTo(compareWith)==0)
            System.out.println("Case Sensitive comparison: String Matches!");
        else
            System.out.println("Case Sensitive comparison: String Doesn't Matches!");
        if(compare.equalsIgnoreCase(compareWith))
            System.out.println("Non-Case Sensitive comparison: String Matches!");
        else
            System.out.println("Non-Case Sensitive comparison: String Mathces!");
    }

    public void indexOf(){
        try{
            System.out.println("Enter any character to view its first and last index: ");
            indexOf=scan.next();
            character=indexOf.charAt(0);
        }
        catch(Exception e){
            System.out.println("Error Code: "+e);
            System.exit(0);
        }
        System.out.println("First Index of \""+character+"\" is: "+(number.indexOf(character))+"\nLast Index: "+(number.lastIndexOf(character)));
    }

    public void caseConversion(){
        System.out.println("Your Name in Upper Case: "+(number.toUpperCase()));
        System.out.println("Your Name in Lower Case: "+(number.toLowerCase()));
    }

    public static void main(String[] args){
        StringLibrary obj=new StringLibrary();
        obj.input();
        obj.compute();
    }
}

Comments

Popular posts from this blog

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...