Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-将字符串中出现的字母计数为26的整数数组_Java_Arrays_String_Letters_Find Occurrences - Fatal编程技术网

java-将字符串中出现的字母计数为26的整数数组

java-将字符串中出现的字母计数为26的整数数组,java,arrays,string,letters,find-occurrences,Java,Arrays,String,Letters,Find Occurrences,我需要采取多行文字和计数的每一行中出现的每个字母。最后一行必须以句点结束。我被要求制作一个长度为26的整数数组,其中arrayName[0]=a的数量,arrayName[1]=b的数量,等等,需要忽略字母大小写。我无法检查每个字母的出现次数,也无法用正确的出现次数定义索引变量。到目前为止,我的代码是: import java.util.Scanner; public class LetterCounter { public static void main(String[] args) {

我需要采取多行文字和计数的每一行中出现的每个字母。最后一行必须以句点结束。我被要求制作一个长度为26的整数数组,其中
arrayName[0]=a的数量
arrayName[1]=b的数量
,等等,需要忽略字母大小写。我无法检查每个字母的出现次数,也无法用正确的出现次数定义索引变量。到目前为止,我的代码是:

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
import java.util.Scanner;
公营信笺台
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
String[]textBlock=新字符串[10];
System.out.println(“输入文本”);
整数指数;
//允许我为所有文本的“行”键入一些内容,直到我用句号结束一行。
用于(索引=0;索引
在循环外声明数组,并在字符匹配时增加该数组的计数

在循环中声明这个

int intialCount = occurrences(charAt(textBlock[index])-49);
occurrences(charAt(textBlock[index])-49) = intialCount++;

您将在occurences[0]中找到“a”的出现,它将返回一个计数。

在循环外声明数组,并在字符匹配时增加该数组的计数

在循环中声明这个

int intialCount = occurrences(charAt(textBlock[index])-49);
occurrences(charAt(textBlock[index])-49) = intialCount++;

您将在引用[0]中找到“a”的引用,该引用将返回一个计数。

对每行的字符引用进行计数。然后把它们打印出来。试试这个

//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]);
}
//最终整数大小=1000;
//String[]textBlock=新字符串[大小];
扫描仪输入=新扫描仪(系统输入);
弦线;
//存储哪个字符出现多少次的频率数组
int[]frequencyArray=新int[26];
数组。填充(frequencyArray,0);
System.out.println(“输入文本:”);
//直到在一行中找到(.)为止,获取输入
//并对当前线路的字符频率进行了统计
while(true){
line=in.nextLine();
对于(int i=0;i
计算每行字符的出现次数。然后把它们打印出来。试试这个

//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]);
}
//最终整数大小=1000;
//String[]textBlock=新字符串[大小];
扫描仪输入=新扫描仪(系统输入);
弦线;
//存储哪个字符出现多少次的频率数组
int[]frequencyArray=新int[26];
数组。填充(frequencyArray,0);
System.out.println(“输入文本:”);
//直到在一行中找到(.)为止,获取输入
//并对当前线路的字符频率进行了统计
while(true){
line=in.nextLine();
对于(int i=0;i
如果不打印调试消息,则只需一行即可完成此工作:

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));
System.out.println(“输入文本行(句点结束):”;
新扫描仪(System.in).useDelimiter(“\\”).next().chars()
.filter(i->i>='a'&&i,收集器.counting())
.forEach((c,n)->System.out.format(“%s的数值:%d\n”,(char)c,n));

如果不打印调试消息,则只需一行即可完成此工作:

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));
System.out.println(“输入文本行(句点结束):”;
新扫描仪(System.in).useDelimiter(“\\”).next().chars()
.filter(i->i>='a'&&i,收集器.counting())
.forEach((c,n)->System.out.format(“%s的数值:%d\n”,(char)c,n));

首先,我认为您希望将数组的声明置于循环之外。你可能想强制你读的每个字母都用小写。然后在“a”到“z”中签入字母后,++读取字母-“a”索引处的数组。这里的问题类似:对于初学者,我认为您希望将数组的声明置于循环之外。您可能希望强制读取每个字母的大小写。然后在“a”到“z”中签入字母后,++读取字母-“a”索引处的数组。类似问题如下: