如何计算数组中的所有字母字符?

时间:2014-11-25 18:13:11

标签: java counter nested-loops

所以基本上我的目标是创建一个程序,它接受用户输入并将其反转并将反转字符作为编码消息打印回用户。现在我需要打印用户输入的字符串的统计信息。 如何计算用户字符串中不同字母的出现次数。到目前为止,我有这个。

   import java.util.*;

  public class SecretCodeMachine
 {
  public static void main(String[]args)
 {
    //object accessing the non static methods
    SecretCodeMachine a = new SecretCodeMachine();

    //input stream
    Scanner in = new Scanner (System.in);
    Scanner i = new Scanner (System.in);

    //prompt the user
    System.out.println("Please input your secret message.");
    String input = in.nextLine();

    //calls the encodedMessage() method; equals the return value to varaible
    String encodedMessage = a.encodeMessage(input);

    //message and prompt
    System.out.println("Encoded message: " + encodedMessage);
    System.out.println("Enter the code in here to get the original message back.");
    String input2 = i.nextLine();

    //if statements saying that if the input equals the encoed message...
    if (input2.equals(encodedMessage))
    {
        //print this
        System.out.println("Original Message: " + input);
    }
    else
    {
        //prints when doesnt equal
        System.out.println("Message not found.");
    }

    //closes the input stream
    i.close();
    in.close();

}
//method for encoding the string from array
public String encodeMessage(String pass)
{
    //passes the parameter string and puts it in an array ()
    char[] toArray = pass.toCharArray();

    for (int i = 0; i < toArray.length; i++) 
    {
        //does the lower case characters
        if (toArray[i] >= 'a' && toArray[i] <= 'z') 
        {
            if (toArray[i] - 'a' <= 13) toArray[i] = (char) ('z' - (toArray[i] - 'a'));
            else toArray[i] = (char) ('a' + ('z' - toArray[i]));
        }

        //does the upper case characters
        else if(toArray[i] >= 'A' && toArray[i] <= 'Z')
        {
            if (toArray[i] - 'A' <= 13) toArray[i] = (char) ('Z' - (toArray[i] - 'A'));
            else toArray[i] = (char) ('A' + ('Z' - toArray[i]));
        }
        //if the characters are non alphatbetic 
        else 
        {
            toArray[i] = toArray[i];
        }
    }

    //converts the toArray back to new string 
    String encodedMessage = new String(toArray);

    //returns the encodedMessage string
    return encodedMessage;
}

}

那么我如何跟踪用户输入的所有字母?

2 个答案:

答案 0 :(得分:0)

使用键作为字符维护映射并计为整数。以下是可以帮助您的方法:

public Map<Character, Integer> getCharCount(String input) {
  Map<Character, Integer> charCountMap = new HashMap<Character, Integer>();
  Integer count;
  for (int i =0; i<input.length();i++) {
    count = charCountMap.get(input.charAt(i));
    if (count == null) {
      charCountMap.put(input.charAt(i), 1);
    } else {
      charCountMap.put(input.charAt(i), count + 1);
    } 
  }
  return charCountMap;
}

答案 1 :(得分:0)

 public class SecretCodeMachine
 {
     HashMap<Character, Integer> charCounts = new HashMap<Character, Integer>();//add hashmap here

更新每个角色的计数代码:

for (int i = 0; i < toArray.length; i++) 
    {
        //does the lower case characters

        //update count for character
        if(charCounts.get(toArray[i]) != null)
        {
            charCounts.put(toArray[i], (charCounts.get(toArray[i]) + 1));
        }
        else
        {
            charCounts.put(toArray[i], 1);
        }

        if (toArray[i] >= 'a' && toArray[i] <= 'z') 
        {
            if (toArray[i] - 'a' <= 13) toArray[i] = (char) ('z' - (toArray[i] - 'a'));
            else toArray[i] = (char) ('a' + ('z' - toArray[i]));
        }

        //does the upper case characters
        else if(toArray[i] >= 'A' && toArray[i] <= 'Z')
        {
            if (toArray[i] - 'A' <= 13) toArray[i] = (char) ('Z' - (toArray[i] - 'A'));
            else toArray[i] = (char) ('A' + ('Z' - toArray[i]));
        }
        //if the characters are non alphatbetic 
        else 
        {
            toArray[i] = toArray[i];
        }
    }

您可以使用散列图来存储字符和整数。键/值对每个映射的每个键只有一个条目。示例