java - 将字符串中出现的字母计数为 26 的 int 数组
java - Count letter occurrences in strings into int array of 26
我需要获取多行文本并计算每行中每个字母的出现次数。最后一行必须以句点结尾。我被要求制作一个长度为 26 的 int 数组,其中 arrayName[0] = number of a's
、arrayName[1] = number of b's
等,并且需要忽略字母大小写。我无法检查每个字母的出现次数并使用正确的出现次数定义索引变量。到目前为止我的代码:
import java.util.Scanner;
public class LetterCounter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] textBlock = new String[10];
System.out.println("Enter text.");
int index;
//Allows me to type in something for all "lines" of text until I end one with a period.
for(index = 0; index < textBlock.length; index++)
{
textBlock[index] = input.nextLine();
if(textBlock[index].contains("."))
{
System.out.println("Period found");
break;
}
}
//Outputs what the user printed except for the lines not typed in (they return null).
System.out.println("Text input by user:");
for(index = 0; index < textBlock.length; index++)
{
if(textBlock[index] != null)
{
System.out.println(textBlock[index]);
}
}
//int array
int[] occurrences = new int[26];
//used to turn letter into number
char letter = 'a' - 49;
char letter2 = 'b' - 49;
char letter3 = 'c' - 49;
//checks that numbers are correct (return "0" for a, "1" for b, and "2" for c)
System.out.println("Number value of a: " + letter);
System.out.println("Number value of b: " + letter2);
System.out.println("Number value of c: " + letter3);
}// End of main
}//End of program
在循环外声明数组并在字符数学运算时增加该数组的计数
在循环内声明这个
int intialCount = occurrences(charAt(textBlock[index])-49);
occurrences(charAt(textBlock[index])-49) = intialCount++;
您会发现 'a' 的出现次数 [0] 将 return 计数。
统计每一行出现的字符。并打印它们。试试这个...
//final int SIZE = 1000;
//String[] textBlock = new String[SIZE];
Scanner in = new Scanner(System.in);
String line;
//frequency array for storing which Character is occurs how many times
int[] frequencyArray = new int[26];
Arrays.fill(frequencyArray, 0);
System.out.println("Enter text : ");
// take input un-till find the (.) in a line
// and also count the frequency of Character of current line
while (true) {
line = in.nextLine();
for (int i = 0; i < line.length(); i++) {
char ch = Character.toLowerCase(line.charAt(i));
if (Character.isLetter(ch)) {
frequencyArray[ch - 'a']++;
}
}
if (line.contains(".")) {
break;
}
}
for (int i = 0; i < 26; i++) {
System.out.println("Total Number of " + (char)(i + 'a') + " : " + frequencyArray[i]);
}
如果不打印调试消息,只需 1 行即可完成工作:
System.out.println("Enter lines of text (period to end):");
new Scanner(System.in).useDelimiter("\.").next().chars()
.filter(i -> i >= 'a' && i <= 'c').boxed()
.collect(Collectors.groupingBy(i -> i, Collectors.counting())
.forEach((c, n) -> System.out.format("Number value of %s: %d\n", (char)c, n));
我需要获取多行文本并计算每行中每个字母的出现次数。最后一行必须以句点结尾。我被要求制作一个长度为 26 的 int 数组,其中 arrayName[0] = number of a's
、arrayName[1] = number of b's
等,并且需要忽略字母大小写。我无法检查每个字母的出现次数并使用正确的出现次数定义索引变量。到目前为止我的代码:
import java.util.Scanner;
public class LetterCounter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String[] textBlock = new String[10];
System.out.println("Enter text.");
int index;
//Allows me to type in something for all "lines" of text until I end one with a period.
for(index = 0; index < textBlock.length; index++)
{
textBlock[index] = input.nextLine();
if(textBlock[index].contains("."))
{
System.out.println("Period found");
break;
}
}
//Outputs what the user printed except for the lines not typed in (they return null).
System.out.println("Text input by user:");
for(index = 0; index < textBlock.length; index++)
{
if(textBlock[index] != null)
{
System.out.println(textBlock[index]);
}
}
//int array
int[] occurrences = new int[26];
//used to turn letter into number
char letter = 'a' - 49;
char letter2 = 'b' - 49;
char letter3 = 'c' - 49;
//checks that numbers are correct (return "0" for a, "1" for b, and "2" for c)
System.out.println("Number value of a: " + letter);
System.out.println("Number value of b: " + letter2);
System.out.println("Number value of c: " + letter3);
}// End of main
}//End of program
在循环外声明数组并在字符数学运算时增加该数组的计数
在循环内声明这个
int intialCount = occurrences(charAt(textBlock[index])-49);
occurrences(charAt(textBlock[index])-49) = intialCount++;
您会发现 'a' 的出现次数 [0] 将 return 计数。
统计每一行出现的字符。并打印它们。试试这个...
//final int SIZE = 1000;
//String[] textBlock = new String[SIZE];
Scanner in = new Scanner(System.in);
String line;
//frequency array for storing which Character is occurs how many times
int[] frequencyArray = new int[26];
Arrays.fill(frequencyArray, 0);
System.out.println("Enter text : ");
// take input un-till find the (.) in a line
// and also count the frequency of Character of current line
while (true) {
line = in.nextLine();
for (int i = 0; i < line.length(); i++) {
char ch = Character.toLowerCase(line.charAt(i));
if (Character.isLetter(ch)) {
frequencyArray[ch - 'a']++;
}
}
if (line.contains(".")) {
break;
}
}
for (int i = 0; i < 26; i++) {
System.out.println("Total Number of " + (char)(i + 'a') + " : " + frequencyArray[i]);
}
如果不打印调试消息,只需 1 行即可完成工作:
System.out.println("Enter lines of text (period to end):");
new Scanner(System.in).useDelimiter("\.").next().chars()
.filter(i -> i >= 'a' && i <= 'c').boxed()
.collect(Collectors.groupingBy(i -> i, Collectors.counting())
.forEach((c, n) -> System.out.format("Number value of %s: %d\n", (char)c, n));