Search code examples
javacounternested-loops

How to count all alphabetical characters in array?


So basically my objective is to make a program that takes the user input and reverses it and prints the inverse character back to user as an encoded message. Right now i need to print the statistics of the user entered string. How do i count the amount of occurrences of different letters from the user string. I have this so far.

   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;
}

}

So how would i keep a track off all the letters that are entered by the user?


Solution

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

    Code for updating count for each character:

    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];
            }
        }
    

    You could use a hashmap to store characters and integers. Key/Value pair only one entry for each key per map. Example