英文:
Taking up some lines of text and analyzing
问题
import java.util.Scanner;
public class LetterCounter {
public static void main(String[] args) {
int frequency = 0;
char character = ' ';
String linesOfText = " ";
int letterTotal = 0;
char[] alphabet = new char[26];
for (char ch = 'a'; ch <= 'z'; ++ch) {
alphabet[ch - 'a'] = ch;
}
System.out.println("Welcome to the Letter Count program.");
System.out.println("Please enter some lines of text followed by a period and a return.");
Scanner keyboard = new Scanner(System.in);
linesOfText = keyboard.nextLine();
if (linesOfText.contains(".")) {
System.out.println("Entry finished.");
}
System.out.println("Letter\t\tFrequency");
for (int i = 0; i < alphabet.length; i++) {
frequency = 0;
for (int j = 0; j < linesOfText.length(); j++) {
character = linesOfText.charAt(j);
if (character == alphabet[i]) {
frequency++;
}
}
System.out.println(alphabet[i] + "\t\t" + frequency);
letterTotal += frequency;
}
System.out.println("Total number of letters: " + letterTotal + ".");
}
}
Note: The provided code is a Java program that analyzes the frequency of letters in a given text input. The program prompts the user to enter multiple lines of text, ending with a period. It then displays the frequency of each letter in alphabetical order. The input handling, text analysis, and output formatting are all part of this code.
英文:
I need to break up a line of text entry into 4 parts to be analyzed. Does anyone know how to code so I take in 4 lines of text, 40 characters per line, and analyze? Thank you for any help on this problem. Here is the algorithm: Algorithm:
*
* 1. User enters multiple lines of text.
* 2. The program will read in the lines of text and display a list of all the
* letters that occur in the text, with the number of times the letter occurs.
* 3. The last line of input should be ended with a period, which serves as a
* sentinel value.
There is a for loop to get the letters of the alphabet and a for loop to record the frequency of alphabet letters in the lines of text.
Problem description:
-
Write a program that will read in multiple lines of text from the user
- and display a list of all the letters that occur in the text, along with the
- number of times each letter occurs.
* -
The last line of input from the user should end with a period, which
- will serve as a sentinel value. Once the last line has been entered, the
- counts for all letters entered by the user should be listed in alphabetical
- order as they are output. Use an array of base type int of length 26, so
- that each indexed variable contains the count of how many letters there are.
- Array indexed variable 0 contains the number of a’s, array indexed variable
- 1 contains the number of b’s and so forth. Allow both uppercase and
- lowercase letters as input, but treat uppercase and lowercase versions of
- the same letter as being equal.
* - Hints: You might find it helpful to define a "helper" method that takes a
- character as an argument and returns an int value that is the correct index
- for that character, such as ‘a’ returning 0, ‘b’ returning 1, and so forth.
- Note that you can use a typecast to change a char to an int, like (int)
- letter. This will not get the number you want, but if you subtract (int)
- 'a', you will then have the right index. Allow the user to repeat this
- task until the user says she or he is finished.
* - A dialog may look something like the following
* - Enter several lines of text to analyze. (Copy and paste to save time.) When
- done, end a line with a period and press return.
- 1: Four score and seven years ago our forefathers
- 2: brought forth upon this continent a new nation,
- 3: conceived in liberty, and dedicated to the
- 4: proposition that all men are created equal.
Yours truly,
Quang Pham
import java.util.Scanner ;
/**
* The Letter Counter program counts the frequency of letters of the
* alphabet in some lines of text. After a period and a return, the computer
* displays the frequency.
*
* @author Quang Pham
* @version Module 8, Homework Project 2, 4/1/20
*
* Algorithm:
*
* 1. User enters multiple lines of text.
* 2. The program will read in the lines of text and display a list of all the
* letters that occur in the text, with the number of times the letter occurs.
* 3. The last line of input should be ended with a period, which serves as a
* sentinel value.
*
* Problem description:
*
* Write a program that will read in multiple lines of text from the user
* and display a list of all the letters that occur in the text, along with the
* number of times each letter occurs.
*
* The last line of input from the user should end with a period, which
* will serve as a sentinel value. Once the last line has been entered, the
* counts for all letters entered by the user should be listed in alphabetical
* order as they are output. Use an array of base type int of length 26, so
* that each indexed variable contains the count of how many letters there are.
* Array indexed variable 0 contains the number of a’s, array indexed variable
* 1 contains the number of b’s and so forth. Allow both uppercase and
* lowercase letters as input, but treat uppercase and lowercase versions of
* the same letter as being equal.
*
* Hints: You might find it helpful to define a "helper" method that takes a
* character as an argument and returns an int value that is the correct index
* for that character, such as ‘a’ returning 0, ‘b’ returning 1, and so forth.
* Note that you can use a typecast to change a char to an int, like (int)
* letter. This will not get the number you want, but if you subtract (int)
* 'a', you will then have the right index. Allow the user to repeat this
* task until the user says she or he is finished.
*
* A dialog may look something like the following
*
* Enter several lines of text to analyze. (Copy and paste to save time.) When
* done, end a line with a period and press return.
* 1: Four score and seven years ago our forefathers
* 2: brought forth upon this continent a new nation,
* 3: conceived in liberty, and dedicated to the
* 4: proposition that all men are created equal.
*
* Here's the counts of characters:
* a: 13
* b: 2
* c: 6
* d: 7
* e: 19
* f: 4
* g: 2
* h: 6
* i: 9
* l: 4
* m: 1
* n: 14
* o: 15
* p: 3
* q: 1
* r: 12
* s: 6
* t: 15
* u: 5
* v: 2
* w: 1
* y: 2
*
* JFK's inaugural quotation: “And so, my fellow Americans: ask not what your
* country can do for you – ask what you can do for your country.”
*
* MLK's Washington speech: "I have a dream that one day this nation will rise
* up and live out the true meaning of its creed: “We hold these truths to be
* self-evident, that all men are created equal.”"
*
* Again, you can submit a single class for this project which contains your
* main method and any helper methods where you feel they can be used.
*
* Along with the file containing your program, submit three print screens or
* screen snips, each with several lines of text entered by the user, and the
* count for each character (a-z).
*/
public class LetterCounter
{
public static void main(String[] args) {
int frequency = 0 ;
char character = ' ' ;
String linesOfText = " " ;
int letterTotal = 0 ;
char[] alphabet = new char[26] ; //new alphabet array
for(char ch = 'a'; ch <= 'z'; ++ch)//fills alphabet array with the alphabet
{
alphabet[ch-'a']=ch ;
}
System.out.println("Welcome to the Letter Count program.") ;
System.out.println("Please enter some lines of text followed by a period and a return.") ;
Scanner keyboard = new Scanner(System.in) ;
linesOfText = keyboard.nextLine() ;
//enter linesOfText into an array and divide it into 4 lines
if (linesOfText.contains(".")) //period sentinel is detected
{
System.out.println("Entry finished.") ;
}
//letter frequency counter
System.out.println("Letter Frequency") ;
for (int i = 0; i < alphabet.length; i++)
{ frequency = 0 ;
for (int j = 0; j < linesOfText.length(); j++)
{
character = linesOfText.charAt(j) ;
if (character == alphabet[i])
{
frequency++ ;
}
}
System.out.println(alphabet[i] + "\t\t" + frequency) ;
letterTotal += frequency ;
}
System.out.println("Total number of letters: " + letterTotal + ".") ;
}
}
答案1
得分: 0
我尝试了这个:
//将linesOfText输入数组并分成1-4行以上进行打印
TextArray
list.addAll(TextArray.asList(linesOfText)) ;
System.out.println("您输入了以下文本行:") ;
for (int i = 0; i < TextArray.length; i++)
{
System.out.print((i + 1) + "." + TextArray.CharAt(0 - 40) ) ;
}
英文:
I tried this:
//enter linesOfText into an array and divide it into 1- 4+ lines and print
TextArray<String> list = new TextArray<linesOfText> ;
list.addAll(TextArray.asList(linesOfText)) ;
System.out.println("You entered these lines of text:") ;
for (int i = 0; i < TextArray.length; i++)
{
System.out.print((i + 1) + "." + TextArray.CharAt(0 - 40) ) ;
}
答案2
得分: 0
我可以使用分隔符输入4行文本。
Scanner keyboard = new Scanner(System.in);
char choice = 'y';
while (choice == 'y')
{
// 使用分隔符
keyboard.useDelimiter("\\.");
keyboard.useDelimiter("\\.\\n");
linesOfText = keyboard.nextLine();
keyboard.next();
英文:
I could use a delimiter to enter 4 lines of text.
Scanner keyboard = new Scanner(System.in);
char choice = 'y';
while (choice == 'y')
{
//use of a delimiter
keyboard.useDelimiter("\\.");
keyboard.useDelimiter("\\.\\n");
linesOfText = keyboard.nextLine() ;
keyboard.next();
答案3
得分: 0
我最终使用了一个包含sentenceChar和某个条目连接成的linesOfText、以及一个句号哨兵和条件检测器的while循环。您喜欢这个解决方案吗?
Scanner keyboard = new Scanner(System.in);
int count = 1;
char sentinel = '.';
char sentenceChar = ' ';
String entry = " ";
while(sentenceChar != sentinel)
{
System.out.print(count + ": ");
entry = keyboard.nextLine();
sentenceChar = entry.charAt(entry.length()-1);
linesOfText = linesOfText + entry;
count++;
if(linesOfText.contains("."))
{
System.out.println("Entry finished.");
break;
}
}
英文:
I finally used a while loop containing sentenceChar and a some entry concatenated into linesOfText and a period sentinel and if detector. Do you like the solution?
Scanner keyboard = new Scanner(System.in);
int count = 1;
char sentinel = '.' ;
char sentenceChar = ' ' ;
String entry = " " ;
while(sentenceChar != sentinel)
{
System.out.print(count + ": ");
entry = keyboard.nextLine();
sentenceChar = entry.charAt(entry.length()-1);
linesOfText = linesOfText + entry ;
count ++;
if(linesOfText.contains("."))
{
System.out.println("Entry finished.") ;
break ;
}
}
答案4
得分: 0
这是整个程序:
import java.util.Scanner;
public class LetterCounter {
public static void main(String[] args) {
int frequency = 0;
char character = ' ';
String linesOfText = " ";
int letterTotal = 0;
char[] alphabet = new char[26];
for (char ch = 'a'; ch <= 'z'; ++ch) {
alphabet[ch - 'a'] = ch;
}
System.out.println("欢迎使用字母计数程序。");
System.out.println("请输入一些文本行,以句号、回车键结束。");
Scanner keyboard = new Scanner(System.in);
int count = 1;
char sentinel = '.';
char sentenceChar = ' ';
String entry = " ";
while (sentenceChar != sentinel) {
System.out.print(count + ": ");
entry = keyboard.nextLine();
sentenceChar = entry.charAt(entry.length() - 1);
linesOfText = linesOfText + entry.toLowerCase();
count++;
if (linesOfText.contains(".")) {
System.out.println("输入结束。");
break;
}
}
System.out.println("字母 频率");
for (int i = 0; i < alphabet.length; i++) {
frequency = 0;
for (int j = 0; j < linesOfText.length(); j++) {
character = linesOfText.charAt(j);
if (character == alphabet[i]) {
frequency++;
}
}
System.out.println(alphabet[i] + "\t\t" + frequency);
letterTotal += frequency;
}
System.out.println("字母总数: " + letterTotal + "。");
}
}
英文:
This is the whole program:
import java.util.Scanner ;
/**
-
The Letter Counter program counts the frequency of letters of the
- alphabet in some lines of text. After a period and a return, the computer
- displays the frequency.
* - @author Quang Pham
- @version Module 8, Homework Project 2, 4/1/20
- Algorithm:
-
- User enters multiple lines of text.
-
- The program will read in the lines of text and display a list of all the
-
letters that occur in the text, with the number of times the letter occurs.
-
- The last line of input should be ended with a period, which serves as a
-
sentinel value.
- Problem description:
-
Write a program that will read in multiple lines of text from the user
- and display a list of all the letters that occur in the text, along with the
- number of times each letter occurs.
* -
The last line of input from the user should end with a period, which
- will serve as a sentinel value. Once the last line has been entered, the
- counts for all letters entered by the user should be listed in alphabetical
- order as they are output. Use an array of base type int of length 26, so
- that each indexed variable contains the count of how many letters there are.
- Array indexed variable 0 contains the number of a’s, array indexed variable
- 1 contains the number of b’s and so forth. Allow both uppercase and
- lowercase letters as input, but treat uppercase and lowercase versions of
- the same letter as being equal.
* - Hints: You might find it helpful to define a "helper" method that takes a
- character as an argument and returns an int value that is the correct index
- for that character, such as ‘a’ returning 0, ‘b’ returning 1, and so forth.
- Note that you can use a typecast to change a char to an int, like (int)
- letter. This will not get the number you want, but if you subtract (int)
- 'a', you will then have the right index. Allow the user to repeat this
- task until the user says she or he is finished.
* - A dialog may look something like the following
* - Enter several lines of text to analyze. (Copy and paste to save time.) When
- done, end a line with a period and press return.
-
1: Four score and seven years ago our forefathers
-
2: brought forth upon this continent a new nation,
-
3: conceived in liberty, and dedicated to the
-
4: proposition that all men are created equal.
*
- Here's the counts of characters:
- a: 13
- b: 2
- c: 6
- d: 7
- e: 19
- f: 4
- g: 2
- h: 6
- i: 9
- l: 4
- m: 1
- n: 14
- o: 15
- p: 3
- q: 1
- r: 12
- s: 6
- t: 15
- u: 5
- v: 2
- w: 1
- y: 2
- JFK's inaugural quotation: “And so, my fellow Americans: ask not what your
- country can do for you – ask what you can do for your country.”
- MLK's Washington speech: "I have a dream that one day this nation will rise
- up and live out the true meaning of its creed: “We hold these truths to be
- self-evident, that all men are created equal.”"
* -
Again, you can submit a single class for this project which contains your
- main method and any helper methods where you feel they can be used.
* - Along with the file containing your program, submit three print screens or
- screen snips, each with several lines of text entered by the user, and the
- count for each character (a-z).
*/
public class LetterCounter
{
public static void main(String[] args)
{
int frequency = 0 ;
char character = ' ' ;
String linesOfText = " " ;
int letterTotal = 0 ;
char[] alphabet = new char[26] ; //new alphabet array
for(char ch = 'a'; ch <= 'z'; ++ch)//fills alphabet array with the alphabet
{
alphabet[ch-'a']=ch ;
}
System.out.println("Welcome to the Letter Count program.") ;
System.out.println("Please enter some lines of text, a period, and a return.") ;
Scanner keyboard = new Scanner(System.in);
int count = 1;
char sentinel = '.' ;
char sentenceChar = ' ' ;
String entry = " " ;
while(sentenceChar != sentinel) //allows entry of several lines of text
{
System.out.print(count + ": ");
entry = keyboard.nextLine();
sentenceChar = entry.charAt(entry.length()-1);
linesOfText = linesOfText + entry.toLowerCase() ; //counts capitalized letters and concatenates
count ++;
if(linesOfText.contains(".")) //detects period
{
System.out.println("Entry finished.") ;
break ;
}
}
//letter frequency counter
System.out.println("Letter Frequency") ;
for (int i = 0; i < alphabet.length; i++)
{ frequency = 0 ;
for (int j = 0; j < linesOfText.length(); j++)
{
character = linesOfText.charAt(j) ;
if (character == alphabet[i])
{
frequency++ ;
}
}
System.out.println(alphabet[i] + "\t\t" + frequency) ;
letterTotal += frequency ;
}
System.out.println("Total number of letters: " + letterTotal + ".") ;
//choice = 'n' ;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论