import java.util.Scanner;public class AuthoringAssistant { .pdf
noelbuddy
15 views
14 slides
Apr 09, 2023
Slide 1 of 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
About This Presentation
import java.util.Scanner;
public class AuthoringAssistant {
// Creates an instance of Scanner class object
private static Scanner scanner = new Scanner(System.in);
/**
*
* The method that replaces two or more sapces with single spaces
* */
private static String shortenSpace(String text) {
String tem...
import java.util.Scanner;
public class AuthoringAssistant {
// Creates an instance of Scanner class object
private static Scanner scanner = new Scanner(System.in);
/**
*
* The method that replaces two or more sapces with single spaces
* */
private static String shortenSpace(String text) {
String temp = text.trim().replaceAll(\" +\", \" \");
return temp;
}
/**
* The method replaces all exclamation marks with character \'.\'
* */
private static String replaceExclamation(String text) {
String temp = text.replaceAll(\"!\", \".\");
return temp;
}
/**
* The method findText that finds the number of occurenence of find in text
* and returns the count
* */
private static int findText(String text, String find) {
int count = 0;
int i = 0;
while ((i = text.indexOf(find)) != -1) {
text = text.substring(i + find.length());
count += 1;
}
return count;
}
/**
* the method takes a string and returns the number of words in the text.
* */
private static int getNumOfWords(String text) {
// call split method that returns the array of words
// split by spaces
text = shortenSpace(text);
String[] words = text.split(\" \");
// return count of words
return words.length;
}
/*
* The method getNumOfNonWSCharacters that remmoves the whitepsaces and
* returns the lenght of the text
*/
private static int getNumOfNonWSCharacters(String text) {
text = text.trim().replaceAll(\"\\\\s\", \"\");
return text.length();
}
/**
* The method printMenu that prints a menu of choices and prompts user to
* enter choice.
* */
private static void printMenu() {
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: \");
}
public static void main(String[] args) {
while(true) {
//prompt for the text
System.out.println(\"Enter a sample text: \");
String text = scanner.nextLine();
//Display user input
System.out.println(\"You entered: \" + text);
//Display menu
printMenu();
char ch = scanner.nextLine().charAt(0);
switch (ch) {
case \'q\':
// close the program
// System.out.println(\"Exit the program\");
System.exit(0);
case \'c\': // Number of non-whitespace characters
int cntNonWhitespaces = getNumOfNonWSCharacters(text);
System.out.println(\"Number of non-whitespace characters: \" + cntNonWhitespaces);
break;
case \'w\': // Number of words
int wordsCount = getNumOfWords(text);
System.out.println(\"Number of words: \" + wordsCount);
break;
case \'f\': // Find text
System.out.println(\"Enter a word or phrase to be found: \");
String find = scanner.nextLine();
int findCount = findText(text, find);
System.out.println(\"\\\"\" + find + \"\\\" instances: \" + findCount);
break;
case \'r\': // Replace all !\'s
String newstring = replaceExclamation(text);
System.out.println(\"Edited text: \" + newstring);
break;
case .
Size: 35.14 KB
Language: en
Added: Apr 09, 2023
Slides: 14 pages
Slide Content
import java.util.Scanner;
public class AuthoringAssistant {
// Creates an instance of Scanner class object
private static Scanner scanner = new Scanner(System.in);
/**
*
* The method that replaces two or more sapces with single spaces
* */
private static String shortenSpace(String text) {
String temp = text.trim().replaceAll(\" +\", \" \");
return temp;
}
/**
* The method replaces all exclamation marks with character \'.\'
* */
private static String replaceExclamation(String text) {
String temp = text.replaceAll(\"!\", \".\");
return temp;
}
/**
* The method findText that finds the number of occurenence of find in text
* and returns the count
* */
private static int findText(String text, String find) {
int count = 0;
int i = 0;
while ((i = text.indexOf(find)) != -1) {
text = text.substring(i + find.length());
count += 1;
}
return count;
}
/**
* the method takes a string and returns the number of words in the text.
* */
private static int getNumOfWords(String text) {
// call split method that returns the array of words
// split by spaces
text = shortenSpace(text);
String[] words = text.split(\" \");
// return count of words
return words.length;
}
/*
* The method getNumOfNonWSCharacters that remmoves the whitepsaces and
* returns the lenght of the text
*/
private static int getNumOfNonWSCharacters(String text) {
text = text.trim().replaceAll(\"\\s\", \"\");
return text.length();
}
/**
* The method printMenu that prints a menu of choices and prompts user to
* enter choice.
* */
private static void printMenu() {
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: \");
}
public static void main(String[] args) {
while(true) {
//prompt for the text
System.out.println(\"Enter a sample text: \");
String text = scanner.nextLine();
//Display user input
System.out.println(\"You entered: \" + text);
//Display menu
printMenu();
char ch = scanner.nextLine().charAt(0);
switch (ch) {
case \'q\':
// close the program
// System.out.println(\"Exit the program\");
System.exit(0);
case \'c\': // Number of non-whitespace characters
int cntNonWhitespaces = getNumOfNonWSCharacters(text);
System.out.println(\"Number of non-whitespace characters: \" + cntNonWhitespaces);
break;
case \'w\': // Number of words
int wordsCount = getNumOfWords(text);
System.out.println(\"Number of words: \" + wordsCount);
break;
case \'f\': // Find text
System.out.println(\"Enter a word or phrase to be found: \");
String find = scanner.nextLine();
int findCount = findText(text, find);
System.out.println(\"\\"\" + find + \"\\" instances: \" + findCount);
break;
case \'r\': // Replace all !\'s
String newstring = replaceExclamation(text);
System.out.println(\"Edited text: \" + newstring);
break;
System.out.println();
}
}
}
SAMPLE OUTPUT:
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.
You entered: 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.
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
Number of non-whitespace characters: 134
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.
You entered: 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.
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
Number of words: 26
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.
You entered: 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.
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 word or phrase to be found:
more
\"more\" instances: 5
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.
You entered: 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.
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 word or phrase to be found:
more shuttle
\"more shuttle\" instances: 2
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.
You entered: 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.
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
Edited 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.
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.
You entered: 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.
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
Edited 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.
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.
You entered: 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.
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
Edited 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.
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.
You entered: 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.
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
import java.util.Scanner;
public class AuthoringAssistant {
// Creates an instance of Scanner class object
private static Scanner scanner = new Scanner(System.in);
/**
*
* The method that replaces two or more sapces with single spaces
* */
private static String shortenSpace(String text) {
String temp = text.trim().replaceAll(\" +\", \" \");
return temp;
}
/**
* The method replaces all exclamation marks with character \'.\'
* */
private static String replaceExclamation(String text) {
String temp = text.replaceAll(\"!\", \".\");
return temp;
}
/**
* The method findText that finds the number of occurenence of find in text
* and returns the count
* */
private static int findText(String text, String find) {
int count = 0;
int i = 0;
while ((i = text.indexOf(find)) != -1) {
text = text.substring(i + find.length());
count += 1;
}
return count;
}
/**
* the method takes a string and returns the number of words in the text.
* */
private static int getNumOfWords(String text) {
// call split method that returns the array of words
// split by spaces
text = shortenSpace(text);
String[] words = text.split(\" \");
// return count of words
return words.length;
}
/*
* The method getNumOfNonWSCharacters that remmoves the whitepsaces and
* returns the lenght of the text
*/
private static int getNumOfNonWSCharacters(String text) {
text = text.trim().replaceAll(\"\\s\", \"\");
return text.length();
}
/**
* The method printMenu that prints a menu of choices and prompts user to
* enter choice.
* */
private static void printMenu() {
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: \");
}
public static void main(String[] args) {
while(true) {
//prompt for the text
System.out.println(\"Enter a sample text: \");
String text = scanner.nextLine();
//Display user input
System.out.println(\"You entered: \" + text);
//Display menu
printMenu();
char ch = scanner.nextLine().charAt(0);
switch (ch) {
case \'q\':
// close the program
// System.out.println(\"Exit the program\");
System.exit(0);
case \'c\': // Number of non-whitespace characters
int cntNonWhitespaces = getNumOfNonWSCharacters(text);
System.out.println(\"Number of non-whitespace characters: \" + cntNonWhitespaces);
break;
case \'w\': // Number of words
int wordsCount = getNumOfWords(text);
System.out.println(\"Number of words: \" + wordsCount);
break;
case \'f\': // Find text
System.out.println(\"Enter a word or phrase to be found: \");
String find = scanner.nextLine();
int findCount = findText(text, find);
System.out.println(\"\\"\" + find + \"\\" instances: \" + findCount);
break;
case \'r\': // Replace all !\'s
String newstring = replaceExclamation(text);
System.out.println(\"Edited text: \" + newstring);
break;
case \'s\': // Shorten spaces
newstring = shortenSpace(text);
System.out.println();
}
}
}
SAMPLE OUTPUT:
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.
You entered: 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.
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
Number of non-whitespace characters: 134
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.
You entered: 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.
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
Number of words: 26
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.
You entered: 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.
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 word or phrase to be found:
more
\"more\" instances: 5
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.
You entered: 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.
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 word or phrase to be found:
more shuttle
\"more shuttle\" instances: 2
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.
You entered: 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.
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
Edited 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.
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.
You entered: 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.
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
Edited 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.
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.
You entered: 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.
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
Edited 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.
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.
You entered: 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.
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