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();
}
}
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