Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7782279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:29:32+00:00 2026-06-01T19:29:32+00:00

I am working on a homework assignment for a class and I have solved

  • 0

I am working on a homework assignment for a class and I have solved almost all of it, but am struggling with one part.

For the assignment, we are supposed to write a program that will count the frequency of occurrence of letters in any given string and then print out a map of them to the console. I have written the program and it works almost correctly, but I cannot get the map to ignore whitespace. It seems to find two different kinds of whitespace as well, one that is the space between words and another that I cannot figure out.

I’ve tried myString.replaceAll(" ", ""); and myString.trim(); to try and eliminate the whitespace before counting the frequency of letters, but it still counts both types of whitespace each time.

Any insight or help is appreciated. I could turn it in like this, but I don’t like half-assing projects. Here is the code:

import java.util.*;

public class LetterFrequency {
   public static void main( String[] args ) {

      Map< String, Integer > myMap = new HashMap< String, Integer >(); 

      createMap( myMap ); 
      displayMap( myMap );
   } 


   private static void createMap( Map< String, Integer > map ) {
      Scanner scanner = new Scanner( System.in ); 
      System.out.println( "Enter a string:" ); 
      String input = scanner.nextLine();

      System.out.println("String: "+input);

      String[] tokens = input.split("");


      for ( String token : tokens ) {
         String letter = token.toLowerCase();


         if ( map.containsKey( letter ) ) {
            int count = map.get( letter ); 
            map.put( letter, count + 1 );
         }
         else 
            map.put( letter, 1 );
      } 
   } 


   private static void displayMap( Map< String, Integer > map ) {      
      Set< String > keys = map.keySet(); 


      TreeSet< String > sortedKeys = new TreeSet< String >( keys );

      System.out.println( "\nMap contains:\nKey\t\tValue" );


      for ( String key : sortedKeys )
         System.out.printf( "%-10s%10s\n", key, map.get( key ) );

      System.out.printf( 
         "\nsize: %d\nisEmpty: %b\n", map.size(), map.isEmpty() );
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:29:34+00:00Added an answer on June 1, 2026 at 7:29 pm

    String.replaceAll should work. Keep in mind that String.replaceAll returns a String. So you have to use the string it returns to perform the rest of your computation.

    For instance, if you have:

    String myString = "hello world";
    myString.replaceAll(" ", "");
    

    myString will still be “hello world”

    You’d want:

    String myNewString = myString.replaceAll(" ", "");
    

    After which myNewString will have no spaces.

    Also, you can simplify your character iteration by using

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

    This will fix your additional “whitespace being counted” problem. This is because when you call myString.split(""), the first element in the returned list is “” (an empty string).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a homework assignment, and I have almost everything done except
I'm working on a homework assignment for Java, where a program is supposed to
I'm working on a homework assignment that requires me to write a program that
I'm working on a homework assignment for CS1, and I almost have it finished
For a homework assignment, I'm working with the following. It's an assigned class structure,
I'm working on a homework assignment (a project), for which one criterion is that
I'm working on a small homework assignment and I'm supposed to make a food
I am working on a homework assignment where I have to implement a MergeSort
I'm working on a homework assignment translating a C program we wrote to MIPS.
I posted a question earlier for a homework assignment but I have a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.