JAVA-在字符串中存储字母和字母数

JAVA-在字符串中存储字母和字母数,java,arrays,for-loop,Java,Arrays,For Loop,我应该要求用户输入一个字符串,我应该解析该字符串并跟踪字母表的编号。就像用户输入字符串“abee” 它将输出显示为: a: 1 b: 1 c: 0 e: 2 到目前为止,我已经能够获取字符串并解析它,并将元素存储到数组中。我可以用for循环一次打印出每封信。现在我面临的问题是,当它打印出字母以及短语中有多少字母时,数字不匹配。例如,如果我输入字母:“abccddee” 它打印出: a: 1 b: 1 c: 1 c: 0 d: 0 d: 0 e: 0 e: 0 出于测试目的,我使用自己的字符串

我应该要求用户输入一个字符串,我应该解析该字符串并跟踪字母表的编号。就像用户输入字符串“abee” 它将输出显示为:

a: 1
b: 1
c: 0
e: 2
到目前为止,我已经能够获取字符串并解析它,并将元素存储到数组中。我可以用for循环一次打印出每封信。现在我面临的问题是,当它打印出字母以及短语中有多少字母时,数字不匹配。例如,如果我输入字母:“abccddee” 它打印出:

a: 1
b: 1
c: 1
c: 0
d: 0
d: 0
e: 0
e: 0
出于测试目的,我使用自己的字符串而不是扫描仪

import java.util.Scanner;

public class CountLetters
{

    public static void main(String[] args)
    {

        //create arrays
        String[] upper = new String[25];
        String[] lowerChar = new String[25];
        int [] lowerCharNum = new int[25];

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter a phrase");

        //grab phrase from user
        String phrase = "abccddee";

        //create array with the size of the phrase entered from user
        String[] letters = new String[phrase.length()];
        System.out.println("letters length: " + letters.length);

        //separate every letter in phrase and store it into array "letters"
        letters = phrase.split("");

        for(int i=0; i<letters.length; i++)
        {
            lowerChar[i] = letters[i];
             switch(letters[i])
             {
               case "a":
                   lowerCharNum[0] += 1;
                   break;
               case "b":
                   lowerCharNum[1] += 1;
                   break;
               case "c":
                   lowerCharNum[2] += 1;
                   break;
               case "d":
                   lowerCharNum[3] += 1;
                   break;
               case "e":
                   lowerCharNum[4] += 1;
                   break;
               case "f":
                   lowerCharNum[5] += 1;
                   break;
            }//end of switch
            System.out.println(lowerChar[i] + ": " + lowerCharNum[i]);
        }
    }//end of main method
}//end of class
import java.util.Scanner;
公营书信
{
公共静态void main(字符串[]args)
{
//创建数组
字符串[]上限=新字符串[25];
字符串[]lowerChar=新字符串[25];
int[]lowerCharNum=新int[25];
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入短语”);
//从用户抓取短语
字符串短语=“abccddee”;
//使用用户输入的短语大小创建数组
String[]字母=新字符串[phrase.length()];
System.out.println(“字母长度:“+字母长度”);
//将短语中的每个字母分开,并将其存储到数组“字母”中
字母=短语。拆分(“”);

for(int i=0;iprint语句必须在while for循环之外

System.out.println(lowerChar[i] + ": " + lowerCharNum[i]);
更新: 您需要首先解析整个字符串,然后开始打印

import java.io.*;
import java.util.*;
 class CountLetters {

    public static void main(String[] args)
    {
int i;
          //create arrays
        String[] upper = new String[25];
        String[] lowerChar = new String[25];
        int [] lowerCharNum = new int[25];


        Scanner input = new Scanner(System.in);

        System.out.println("Please enter a phrase");

        //grab phrase from user
        String phrase = "abccddee";

        //create array with the size of the phrase entered from user
        String[] letters = new String[phrase.length()];
        System.out.println("letters length: " + letters.length);

        //seperate every letter in phrase and store it into array "letters"
        letters = phrase.split("");


        for(i=0; i<letters.length; i++)
        {
            lowerChar[i] = letters[i];
        switch(letters[i])
         {
         case "a":
            lowerCharNum[0] += 1;
             break;
         case "b":
            lowerCharNum[1] += 1;
             break;
         case "c":
            lowerCharNum[2] += 1;
             break;
         case "d":
            lowerCharNum[3] += 1;
             break;
         case "e":
            lowerCharNum[4] += 1;
             break;
         case "f":
            lowerCharNum[5] += 1;
             break;
         }//end of switch



        }

for(i=0;i<5;i++)
System.out.println(lowerChar[i] + ": " + lowerCharNum[i]);



    }//end of main method


}//end of class
import java.io.*;
导入java.util.*;
类计数字母{
公共静态void main(字符串[]args)
{
int i;
//创建数组
字符串[]上限=新字符串[25];
字符串[]lowerChar=新字符串[25];
int[]lowerCharNum=新int[25];
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入短语”);
//从用户抓取短语
字符串短语=“abccddee”;
//使用用户输入的短语大小创建数组
String[]字母=新字符串[phrase.length()];
System.out.println(“字母长度:“+字母长度”);
//将短语中的每个字母分开,并将其存储到数组“字母”中
字母=短语。拆分(“”);

for(i=0;i您正在for循环内打印。您应该在该循环外打印频率

您使用的方法不可扩展。鉴于短语仅由大写和小写英文字母组成,您必须在交换机中编写52个大小写语句

一个更好的方法是将编码用于您的目的。您可以在以下行中使用一些内容:

int frequency[] = new int[128];
for (int i = 0; i < phrase.length(); i++) {
    frequency[(int) phrase.charAt(i)]++;
}
int frequency[]=新int[128];
for(int i=0;i

在此方法中,
频率
数组用于计算
短语
字符串中前128个ASCII字符的出现次数。操作
(int)短语.charAt(i)
只需将字符转换为相应的ASCII码,我们将该字符的计数器增加1。在处理结束时,
频率
数组将包含给定
短语
字符串中前128个ASCII字符的出现次数。只需打印此频率即可获得所需的输出

使用数组的解决方案有点复杂。通过使用映射,我们可以直接将遇到的字符与它们遇到的次数相关联,从而使增加计数器和输出计数器变得非常简单,而不必在不同数组中查找索引

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CountLetters
{

    public static void main(String[] args)
    {

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter a phrase");

        //grab phrase from user
        String phrase = "abccddee";

        //create array with the phrase entered from user
        char[] letters = phrase.toCharArray();
        System.out.println("letters length: " + letters.length);

        // Map to keep track of all encountered characters and the
        // number of times we've encountered them
        Map<Character, Integer> characterCounts = new HashMap<>();
        for(int i=0; i<letters.length; i++)
        {
            Character character = letters[i];
            if(characterCounts.containsKey(character))
            {
                // We've encountered this character before, increase the counter
                characterCounts.put(character, characterCounts.get(character) + 1);
            }
            else
            {
                // This is the first time we encounter this character
                characterCounts.put(lowerChar, 1);
            }
        }

        // Iterate over all character-counter pairs and print them
        for(Map.Entry<Character, Integer> entry : characterCounts.entrySet())
        {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }//end of main method
}//end of class
import java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
公营书信
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入短语”);
//从用户抓取短语
字符串短语=“abccddee”;
//使用用户输入的短语创建数组
char[]字母=短语.toCharArray();
System.out.println(“字母长度:“+字母长度”);
//映射以跟踪遇到的所有字符和
//我们遇到他们的次数
Map characterCounts=new HashMap();

对于(int i=0;i,您可以使用java的
集合
哈希映射
,而不是使用简单数组

使用
HashMap
时,主要工作是使用
for
循环,检查
字符是否已经存在于
HashMap
中,如果已经存在,我们将获得与
字符相关的值,并将现有值加1,如果
字符
不存在,我们将将在
HashMap
中放置
字符
,并将初始计数1与相关字符一起存储

        HashMap<Character, Integer> lettersCount = new HashMap<>();
        String phrase = "abccddee";
        int length = phrase.length();
        int count = 1;
        for (int i = 0; i < length; i++) {
        int integer = 0;
        char charAt = input.charAt(i);
        if (!lettersCount.containsKey(charAt)) {
            lettersCount.put(charAt, 0);
        }
        integer = lettersCount.get(charAt);
        integer = initialCount + integer;
        lettersCount.put(charAt, integer);
    }
    System.out.println(lettersCount);
HashMap letterscont=newhashmap();
字符串短语=“abccddee”;
int length=短语.length();
整数计数=1;
for(int i=0;i
您使用的是需要在声明时首先初始化的数组,这将创建一个额外的内存空间,如果没有遇到所有26个字母,这将是浪费的,并且根据您在问题中提供的代码,您正在分配3个数组,因此它将占用更多内存,因此此解决方案只需要a
HashMap
HashMappublic class Test {

    public static void main(String[] args) {

        //Taking the input from the user
        System.out.println("Enter the String"); //I am entering "abccddee" for your example
        Scanner sc = new Scanner(System.in);
        String input = sc.next(); 

        //LinkedhashMap preserves the order in which input was supplied
        LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<Character, Integer>();

        for(int i=0; i<input.length(); i++){

            //method get will return null if the letter is not available at the given index else it will return the count
            Integer j = lhm.get(input.charAt(i));

            //If the chracter was not present in the String
            if(j==null)
                lhm.put(input.charAt(i),1);
            //If the character was present in the String
             else
                lhm.put(input.charAt(i),j+1);

         }

         for(Character c : lhm.keySet())  
             System.out.println(c+": "+lhm.get(c)+" ");

    }

}
a: 1 
b: 1 
c: 2 
d: 2 
e: 2