SlideShare a Scribd company logo
1 of 14
Download to read offline
/**
* 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':
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(" +", " ");
}
/**
* 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();
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***");
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
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
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
* 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();
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) {
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;
}
/**
* 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 ");
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
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
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

More Related Content

Similar to The java program that display a menu of choices and p.pdf

131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdfarshiartpalace
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfezonesolutions
 
6 c control statements branching & jumping
6 c control statements branching & jumping6 c control statements branching & jumping
6 c control statements branching & jumpingMomenMostafa
 
I Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfI Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfallystraders
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
4 operators, expressions & statements
4  operators, expressions & statements4  operators, expressions & statements
4 operators, expressions & statementsMomenMostafa
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxJStalinAsstProfessor
 

Similar to The java program that display a menu of choices and p.pdf (18)

131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
c programming
c programmingc programming
c programming
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
String
StringString
String
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
M C6java7
M C6java7M C6java7
M C6java7
 
Lecture 3.pptx
Lecture 3.pptxLecture 3.pptx
Lecture 3.pptx
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
6 c control statements branching & jumping
6 c control statements branching & jumping6 c control statements branching & jumping
6 c control statements branching & jumping
 
Team 1
Team 1Team 1
Team 1
 
C programs
C programsC programs
C programs
 
I Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdfI Have the following Java program in which converts Date to Words an.pdf
I Have the following Java program in which converts Date to Words an.pdf
 
2 data and c
2 data and c2 data and c
2 data and c
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
4 operators, expressions & statements
4  operators, expressions & statements4  operators, expressions & statements
4 operators, expressions & statements
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 

More from annaistrvlr

VideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdfVideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdfannaistrvlr
 
Use the order of operations to simplify the expression. Parenthese.pdf
Use the order of operations to simplify the expression. Parenthese.pdfUse the order of operations to simplify the expression. Parenthese.pdf
Use the order of operations to simplify the expression. Parenthese.pdfannaistrvlr
 
The RASopathies are a clinically defined group of medical genetic sy.pdf
The RASopathies are a clinically defined group of medical genetic sy.pdfThe RASopathies are a clinically defined group of medical genetic sy.pdf
The RASopathies are a clinically defined group of medical genetic sy.pdfannaistrvlr
 
The Brocas area is located in the frontal part of the brain on the.pdf
The Brocas area is located in the frontal part of the brain on the.pdfThe Brocas area is located in the frontal part of the brain on the.pdf
The Brocas area is located in the frontal part of the brain on the.pdfannaistrvlr
 
the conflict of internet surveilance versus individuals and privacy .pdf
the conflict of internet surveilance versus individuals and privacy .pdfthe conflict of internet surveilance versus individuals and privacy .pdf
the conflict of internet surveilance versus individuals and privacy .pdfannaistrvlr
 
Question Tell me about yourselfAnswer We begin by introducing .pdf
Question Tell me about yourselfAnswer  We begin by introducing .pdfQuestion Tell me about yourselfAnswer  We begin by introducing .pdf
Question Tell me about yourselfAnswer We begin by introducing .pdfannaistrvlr
 
Pls post the question again, i cant see any.SolutionPls post t.pdf
Pls post the question again, i cant see any.SolutionPls post t.pdfPls post the question again, i cant see any.SolutionPls post t.pdf
Pls post the question again, i cant see any.SolutionPls post t.pdfannaistrvlr
 
Please find the summary belowContemporary geographical conditions.pdf
Please find the summary belowContemporary geographical conditions.pdfPlease find the summary belowContemporary geographical conditions.pdf
Please find the summary belowContemporary geographical conditions.pdfannaistrvlr
 
IFRS 13Fair Value Measurement applies to IFRSs that require or per.pdf
IFRS 13Fair Value Measurement applies to IFRSs that require or per.pdfIFRS 13Fair Value Measurement applies to IFRSs that require or per.pdf
IFRS 13Fair Value Measurement applies to IFRSs that require or per.pdfannaistrvlr
 
Cyanobacteria does not compete with methanogens , rather it acts as .pdf
Cyanobacteria does not compete with methanogens , rather it acts as .pdfCyanobacteria does not compete with methanogens , rather it acts as .pdf
Cyanobacteria does not compete with methanogens , rather it acts as .pdfannaistrvlr
 
Correlation is the key in diversification of risk. If one asset does.pdf
Correlation is the key in diversification of risk. If one asset does.pdfCorrelation is the key in diversification of risk. If one asset does.pdf
Correlation is the key in diversification of risk. If one asset does.pdfannaistrvlr
 
Ans.) Exosomes are layer enclosed vesicles effectively discharged in.pdf
Ans.) Exosomes are layer enclosed vesicles effectively discharged in.pdfAns.) Exosomes are layer enclosed vesicles effectively discharged in.pdf
Ans.) Exosomes are layer enclosed vesicles effectively discharged in.pdfannaistrvlr
 
(a).Let me show that H is a subgroup. Then you can make the identica.pdf
(a).Let me show that H is a subgroup. Then you can make the identica.pdf(a).Let me show that H is a subgroup. Then you can make the identica.pdf
(a).Let me show that H is a subgroup. Then you can make the identica.pdfannaistrvlr
 
1=2Solution 1=2.pdf
 1=2Solution 1=2.pdf 1=2Solution 1=2.pdf
1=2Solution 1=2.pdfannaistrvlr
 
ASSETS 2016 2015 Current assets Cash 3251 3407 Accouns re.pdf
    ASSETS 2016 2015   Current assets     Cash 3251 3407   Accouns re.pdf    ASSETS 2016 2015   Current assets     Cash 3251 3407   Accouns re.pdf
ASSETS 2016 2015 Current assets Cash 3251 3407 Accouns re.pdfannaistrvlr
 
ACA (Affordable care Act) signed by Obama on 23 march 2010. .pdf
     ACA (Affordable care Act) signed by Obama on 23 march 2010.      .pdf     ACA (Affordable care Act) signed by Obama on 23 march 2010.      .pdf
ACA (Affordable care Act) signed by Obama on 23 march 2010. .pdfannaistrvlr
 
we have, V1 T1= V2T2 Volume = V2 = 12.126 lit.pdf
                     we have,  V1 T1= V2T2  Volume = V2 = 12.126 lit.pdf                     we have,  V1 T1= V2T2  Volume = V2 = 12.126 lit.pdf
we have, V1 T1= V2T2 Volume = V2 = 12.126 lit.pdfannaistrvlr
 
Should just be proportional .0951.28=x1 .074=.pdf
                     Should just be proportional  .0951.28=x1 .074=.pdf                     Should just be proportional  .0951.28=x1 .074=.pdf
Should just be proportional .0951.28=x1 .074=.pdfannaistrvlr
 
only C represents the pair of resonance structure.pdf
                     only C represents the pair of resonance structure.pdf                     only C represents the pair of resonance structure.pdf
only C represents the pair of resonance structure.pdfannaistrvlr
 
pH =1.074 pH = - log(concentration of H+) = -log .pdf
                     pH =1.074 pH = - log(concentration of H+) = -log .pdf                     pH =1.074 pH = - log(concentration of H+) = -log .pdf
pH =1.074 pH = - log(concentration of H+) = -log .pdfannaistrvlr
 

More from annaistrvlr (20)

VideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdfVideoGamepublic class VideoGam.pdf
VideoGamepublic class VideoGam.pdf
 
Use the order of operations to simplify the expression. Parenthese.pdf
Use the order of operations to simplify the expression. Parenthese.pdfUse the order of operations to simplify the expression. Parenthese.pdf
Use the order of operations to simplify the expression. Parenthese.pdf
 
The RASopathies are a clinically defined group of medical genetic sy.pdf
The RASopathies are a clinically defined group of medical genetic sy.pdfThe RASopathies are a clinically defined group of medical genetic sy.pdf
The RASopathies are a clinically defined group of medical genetic sy.pdf
 
The Brocas area is located in the frontal part of the brain on the.pdf
The Brocas area is located in the frontal part of the brain on the.pdfThe Brocas area is located in the frontal part of the brain on the.pdf
The Brocas area is located in the frontal part of the brain on the.pdf
 
the conflict of internet surveilance versus individuals and privacy .pdf
the conflict of internet surveilance versus individuals and privacy .pdfthe conflict of internet surveilance versus individuals and privacy .pdf
the conflict of internet surveilance versus individuals and privacy .pdf
 
Question Tell me about yourselfAnswer We begin by introducing .pdf
Question Tell me about yourselfAnswer  We begin by introducing .pdfQuestion Tell me about yourselfAnswer  We begin by introducing .pdf
Question Tell me about yourselfAnswer We begin by introducing .pdf
 
Pls post the question again, i cant see any.SolutionPls post t.pdf
Pls post the question again, i cant see any.SolutionPls post t.pdfPls post the question again, i cant see any.SolutionPls post t.pdf
Pls post the question again, i cant see any.SolutionPls post t.pdf
 
Please find the summary belowContemporary geographical conditions.pdf
Please find the summary belowContemporary geographical conditions.pdfPlease find the summary belowContemporary geographical conditions.pdf
Please find the summary belowContemporary geographical conditions.pdf
 
IFRS 13Fair Value Measurement applies to IFRSs that require or per.pdf
IFRS 13Fair Value Measurement applies to IFRSs that require or per.pdfIFRS 13Fair Value Measurement applies to IFRSs that require or per.pdf
IFRS 13Fair Value Measurement applies to IFRSs that require or per.pdf
 
Cyanobacteria does not compete with methanogens , rather it acts as .pdf
Cyanobacteria does not compete with methanogens , rather it acts as .pdfCyanobacteria does not compete with methanogens , rather it acts as .pdf
Cyanobacteria does not compete with methanogens , rather it acts as .pdf
 
Correlation is the key in diversification of risk. If one asset does.pdf
Correlation is the key in diversification of risk. If one asset does.pdfCorrelation is the key in diversification of risk. If one asset does.pdf
Correlation is the key in diversification of risk. If one asset does.pdf
 
Ans.) Exosomes are layer enclosed vesicles effectively discharged in.pdf
Ans.) Exosomes are layer enclosed vesicles effectively discharged in.pdfAns.) Exosomes are layer enclosed vesicles effectively discharged in.pdf
Ans.) Exosomes are layer enclosed vesicles effectively discharged in.pdf
 
(a).Let me show that H is a subgroup. Then you can make the identica.pdf
(a).Let me show that H is a subgroup. Then you can make the identica.pdf(a).Let me show that H is a subgroup. Then you can make the identica.pdf
(a).Let me show that H is a subgroup. Then you can make the identica.pdf
 
1=2Solution 1=2.pdf
 1=2Solution 1=2.pdf 1=2Solution 1=2.pdf
1=2Solution 1=2.pdf
 
ASSETS 2016 2015 Current assets Cash 3251 3407 Accouns re.pdf
    ASSETS 2016 2015   Current assets     Cash 3251 3407   Accouns re.pdf    ASSETS 2016 2015   Current assets     Cash 3251 3407   Accouns re.pdf
ASSETS 2016 2015 Current assets Cash 3251 3407 Accouns re.pdf
 
ACA (Affordable care Act) signed by Obama on 23 march 2010. .pdf
     ACA (Affordable care Act) signed by Obama on 23 march 2010.      .pdf     ACA (Affordable care Act) signed by Obama on 23 march 2010.      .pdf
ACA (Affordable care Act) signed by Obama on 23 march 2010. .pdf
 
we have, V1 T1= V2T2 Volume = V2 = 12.126 lit.pdf
                     we have,  V1 T1= V2T2  Volume = V2 = 12.126 lit.pdf                     we have,  V1 T1= V2T2  Volume = V2 = 12.126 lit.pdf
we have, V1 T1= V2T2 Volume = V2 = 12.126 lit.pdf
 
Should just be proportional .0951.28=x1 .074=.pdf
                     Should just be proportional  .0951.28=x1 .074=.pdf                     Should just be proportional  .0951.28=x1 .074=.pdf
Should just be proportional .0951.28=x1 .074=.pdf
 
only C represents the pair of resonance structure.pdf
                     only C represents the pair of resonance structure.pdf                     only C represents the pair of resonance structure.pdf
only C represents the pair of resonance structure.pdf
 
pH =1.074 pH = - log(concentration of H+) = -log .pdf
                     pH =1.074 pH = - log(concentration of H+) = -log .pdf                     pH =1.074 pH = - log(concentration of H+) = -log .pdf
pH =1.074 pH = - log(concentration of H+) = -log .pdf
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
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