Hi need help in this intro java hw Please DO NOT use advance
Hi need help in this
intro java hw. Please DO NOT use advanced concepts like array of strings and StringbUilder. these are the instructions and the things to avoid so i do not lose points on the homework. Thanks!
Solution
StringOperations.java
import java.util.Scanner;
public class StringOperations {
public static void main(String[] args) {
//Declaring variables
String str1=null,str2=null;
char char1;
int position;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the first string entered by the user
System.out.print(\"Enter First String :\");
str1=sc.next();
//Getting the second string entered by the user
System.out.print(\"Enter Second String :\");
str2=sc.next();
//Comparing the two strings
int i=str1.compareTo(str2);
if(i>0)
{
System.out.println(\"\'\"+str1+\"\' comes before \'\"+str2+\"\'\");
}
else if(i<0)
{
System.out.println(\"\'\"+str1+\"\' comes after \'\"+str2+\"\'\");
}
else if(i==0)
{
System.out.println(\"\'\"+str1+\"\' is equal to \'\"+str2+\"\'\");
}
//Converting the string str1 into uppercase and displaying
System.out.println(\"UpperCase of \'\"+str1+\"\' is :\"+str1.toUpperCase());
//Converting the string str2 into lowercase and displaying
System.out.println(\"Lowercase of \'\"+str2+\"\' is: \"+str2.toLowerCase());
//Count the number of characters in the string str1
System.out.println(\"Number of characters in \'\"+str1+\"\' is :\"+str1.length());
//Displaying the first character in the string str2
System.out.println(\"First Character in \'\"+str2+\"\' is: \"+str2.charAt(0));
//Getting the character from the user
System.out.print(\"Enter the Character you want to Search for in \'\"+str1+\"\' :\");
char1=sc.next(\".\").charAt(0);
/* calling the method to count the number of occurrences
* of the character in the string str1 by passing the string str1
* and character as inputs
*/
int num=countOccurrences(str1,char1);
//Displaying the number of occurrences of the character
System.out.println(\"The Number of Occurrences of \'\"+char1+\"\' in \'\"+str1+\"\' is :\"+num);
//Getting the posotion number from the user
System.out.print(\"Enter the Position number to find the character in \'\"+str1+\"\' :\");
position=sc.nextInt();
//calling the method showChar()
char ch=showChar(str1,position);
//Displaying the character occurred in the position of the string str1
System.out.println(\"The Position \"+position+\" of the character \'\"+char1+\"\' in the string \'\"+str1+\"\' is :\"+ch);
}
/*
* This method will finds the character at the position
* Params : string str1,position
* Return : character
*/
private static char showChar(String str1, int position) {
//Declaring local variable
char ch=0;
//Getting the character at position
ch=str1.charAt(position);
return ch;
}
/*
* This method will finds the number of occurrences of a charcater in a string
* Params : string str1,character
* Return : integer
*/
private static int countOccurrences(String str1, char char1) {
//Declaring local variables
int count=0;
/* This for loop will count the number of
* occurrences of a character in the string
*/
for(int i=0;i<str1.length();i++)
{
if(str1.charAt(i)==char1)
{
//If found incrementing the count value by 1 every time
count++;
}
}
return count;
}
}
_________________________________________
Output:
Enter First String :williams
Enter Second String :thomson
\'williams\' comes before \'thomson\'
UpperCase of \'williams\' is :WILLIAMS
Lowercase of \'thomson\' is: thomson
Number of characters in \'williams\' is :8
First Character in \'thomson\' is: t
Enter the Character you want to Search for in \'williams\' :l
The Number of Occurrences of \'l\' in \'williams\' is :2
Enter the Position number to find the character in \'williams\' :5
The Position 5 of the character \'l\' in the string \'williams\' is :a
_______________________Thank You


