The java program displays a menu allowing the user to choose operations on text such as counting characters, words, replacing text, and shortening spaces. The user enters a sample text and chooses an operation which calls the corresponding method. The method performs the operation and prints the results.
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
The java program that display a menu of choices and p.pdf
1. /**
* The java program that display a menu of choices
* and prompt user choice and then call the corresponding
* method and then prints the results to console.
* */
//TextOperations.java
import java.util.Scanner;
public class TextOperations {
private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)
{
char ch;
String text;
while(true)
{
ch=printMenu();
switch (ch)
{
case 'c':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
int numChars=getNumOfNonWSCharacters(text);
System.out.println("Number of characters : "+numChars);
break;
case 'w':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
int numWords=getNumOfWords(text);
System.out.println("Number of words:"+numWords);
break;
case 'f':
2. System.out.println("Enter a sample text:");
text=scanner.nextLine();
System.out.println("Enter a word or phrase to be found:");
String search=scanner.nextLine();
int count=findText(text,search);
System.out.println("more "+search+" instances:"+count);
break;
case 'r':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
String result=replaceExclamation(text);
System.out.println("Result : "+result);
break;
case 's':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
String shortenText=shortenSpace(text);
System.out.println(shortenText);
break;
case 'q':
System.out.println("Good bye!");
System.exit(0);
}
}
}
/*
* The method that replaces 2 or more spaces
* witht single space
* */
private static String shortenSpace(String text) {
String temp=text;
return temp.trim().replaceAll(" +", " ");
}
/**
3. * The method that takes text as input and
* replaces the exclamation with .
* */
private static String replaceExclamation(String text) {
String temp=text;
temp=temp.replace('!', '.');
return temp;
}
/**
* The method that takes source text and search
* string and returns the number of search words
* find in the source text
* */
private static int findText(String text, String search) {
String temp=text.trim();
int count=0;
temp=temp.trim();
String[] arr=temp.split(" ");
for (String string : arr)
{
if(string.contains(search))
count++;
}
return count;
}
/**
* The method that returns the number of words in a text
* */
private static int getNumOfWords(String text) {
String temp=text.trim();
4. temp=temp.trim();
String[] arr=temp.split(" ");
return arr.length;
}
/**
* The method that returns the number of non-white
* space characters in a given string
* */
private static int getNumOfNonWSCharacters(String text) {
String temp=text.trim();
int countChars=0;
temp=temp.trim();
String[] arr=temp.split(" ");
for (String string : arr)
{
countChars+=string.length();
}
return countChars;
}
/**
* The method prompts user to enter user choice and returns
* user choice
* */
private static char printMenu() {
char ch;
do
{
System.out.println("***MENU***");
5. System.out.println("c - Number of non-whitespace characters ");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s ");
System.out.println("s - Shorten spaces ");
System.out.println("q - Quit");
System.out.println("Choose an option:");
ch=scanner.nextLine().charAt(0);
if(ch!='c' && ch!='w' && ch!='f' && ch!='r' && ch!='s' && ch!='q')
System.out.println("Invalid character");
}while(ch!='c' && ch!='w' && ch!='f' && ch!='r' && ch!='s' && ch!='q');
return ch;
}
}//end of class
Sample output:
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
c
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Number of characters : 181
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
6. s - Shorten spaces
q - Quit
Choose an option:
w
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Number of words:35
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
f
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Enter a word or phrase to be found:
more
more more instances:5
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
r
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
7. and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Result : We'll continue our quest in space. There will be more shuttle flights and more shuttle
crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our
hopes and our journeys continue.
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
s
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
q
Solution
/**
* The java program that display a menu of choices
8. * and prompt user choice and then call the corresponding
* method and then prints the results to console.
* */
//TextOperations.java
import java.util.Scanner;
public class TextOperations {
private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)
{
char ch;
String text;
while(true)
{
ch=printMenu();
switch (ch)
{
case 'c':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
int numChars=getNumOfNonWSCharacters(text);
System.out.println("Number of characters : "+numChars);
break;
case 'w':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
int numWords=getNumOfWords(text);
System.out.println("Number of words:"+numWords);
break;
case 'f':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
System.out.println("Enter a word or phrase to be found:");
String search=scanner.nextLine();
9. int count=findText(text,search);
System.out.println("more "+search+" instances:"+count);
break;
case 'r':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
String result=replaceExclamation(text);
System.out.println("Result : "+result);
break;
case 's':
System.out.println("Enter a sample text:");
text=scanner.nextLine();
String shortenText=shortenSpace(text);
System.out.println(shortenText);
break;
case 'q':
System.out.println("Good bye!");
System.exit(0);
}
}
}
/*
* The method that replaces 2 or more spaces
* witht single space
* */
private static String shortenSpace(String text) {
String temp=text;
return temp.trim().replaceAll(" +", " ");
}
/**
* The method that takes text as input and
* replaces the exclamation with .
* */
private static String replaceExclamation(String text) {
10. String temp=text;
temp=temp.replace('!', '.');
return temp;
}
/**
* The method that takes source text and search
* string and returns the number of search words
* find in the source text
* */
private static int findText(String text, String search) {
String temp=text.trim();
int count=0;
temp=temp.trim();
String[] arr=temp.split(" ");
for (String string : arr)
{
if(string.contains(search))
count++;
}
return count;
}
/**
* The method that returns the number of words in a text
* */
private static int getNumOfWords(String text) {
String temp=text.trim();
temp=temp.trim();
String[] arr=temp.split(" ");
return arr.length;
}
11. /**
* The method that returns the number of non-white
* space characters in a given string
* */
private static int getNumOfNonWSCharacters(String text) {
String temp=text.trim();
int countChars=0;
temp=temp.trim();
String[] arr=temp.split(" ");
for (String string : arr)
{
countChars+=string.length();
}
return countChars;
}
/**
* The method prompts user to enter user choice and returns
* user choice
* */
private static char printMenu() {
char ch;
do
{
System.out.println("***MENU***");
System.out.println("c - Number of non-whitespace characters ");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s ");
12. System.out.println("s - Shorten spaces ");
System.out.println("q - Quit");
System.out.println("Choose an option:");
ch=scanner.nextLine().charAt(0);
if(ch!='c' && ch!='w' && ch!='f' && ch!='r' && ch!='s' && ch!='q')
System.out.println("Invalid character");
}while(ch!='c' && ch!='w' && ch!='f' && ch!='r' && ch!='s' && ch!='q');
return ch;
}
}//end of class
Sample output:
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
c
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Number of characters : 181
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
w
13. Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Number of words:35
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
f
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Enter a word or phrase to be found:
more
more more instances:5
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
r
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
Result : We'll continue our quest in space. There will be more shuttle flights and more shuttle
crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our
14. hopes and our journeys continue.
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
s
Enter a sample text:
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
We'll continue our quest in space. There will be more shuttle flights and more shuttle crews
and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes
and our journeys continue!
***MENU***
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
q