Answered Essay: The main goal of this first homework is to complete a relatively straightforward Jav

The main goal of this first homework is to complete a relatively straightforward Java program by taking advantage of your Java knowledge.

Problem description:  Given a string variable String s; , initialize s such that it contains a paragraph in English text. You can do so within your program by reading the initial value from the keyboard by using the Scanner class. Furthermore, this paragraph consists of no more than 100 tokens. Tokens are sequences of contiguous characters separated by any of the specified delimiters (e.g., white spaces, commas(,) and period(.)). Please implement a Java program to perform the following two tasks on s:

Implement the function void getLetterFreq(string s); to identify the frequency of each unique letter (‘a’-‘z’, case insensitive) in s. This function will call the “System.out.println” statement(s) to print out each letter frequencies in the string on the screen.

Implement the function  void StrToTokens(string s); to identify and print all the tokens contained in s on the standard output . For this assignment, only white spaces, commas and periods will be considered as delimiters. For instance, the string “Hi, welcome to CSC 220.It is your first assignment” contains Ten tokens “Hi”, “welcome”, “to”, “CSC”, “220”, “It”, “is”, “your”, “first”, “assignment”. You are not allowed to call existing library methods for this task. Specifically, you are required to loop through the input string one character at a time to separate the input string to different tokens. Please store the tokens in an array (such as ArrayList<String>) before printing out all the tokens.

Implement the main() method that (1) declares and initializes the string s, and (2) calls the above two methods.

Example input and output:

Suppose s=”Always remember that you are unique. Just like everyone else.”

The method getLetterFreq(s) will print out the following information (yours might be in a different format):

{‘a’: 4, ‘b’: 1, ‘e’: 11, ‘i’: 2, ‘h’: 1, ‘k’: 1, ‘j’: 1, ‘m’: 2, ‘l’: 3, ‘o’: 2, ‘n’: 2, ‘q’: 1, ‘s’: 3, ‘r’: 4, ‘u’: 4, ‘t’: 3, ‘w’: 1, ‘v’: 1, ‘y’: 3}

The output of  StrToTokens(s);  will be ‘Always’, ‘remember’, ‘that’, ‘you’, ‘are’, ‘unique.’, ‘Just’, ‘like’, ‘everyone’, ‘else.’

Expert Answer



  • Code in Java is given below.
  • Both functions are implemented and tested.
  • Each and every step is explained well in code itself using comments.
  • Read all the comments for better understanding of the code.
  • Output screenshot is provided below at the end of code.
  • Both functions are declared as static function so that there is no need to create object of class.
  • Note class name is StringFrequency so save the file as

Code in java::

import java.util.*;

public class StringFrequency{

public static void main(String[] args){


* Here we ask user to enter the string s


Scanner sc=new Scanner(;

System.out.println(“Please enter string s ::”);

String s=sc.nextLine();


* Now calling the method getLetterFreq().

* Note that we have passed string s as parameter.


System.out.println(“nFrequency of each character in string s is ::”);



* CALLING the function StrToTokens();


System.out.println(“nTokens in string s are ::”);


}// main ends here

public static void getLetterFreq(String s){


* Converting string s to lower case for simplicity.

* We store count of lower alphabets in integer array given below.


String s1=s.toLowerCase();


* An integer array named count is created of size 26

* for 26 characters of alphabets.

* At index 0 count of character ‘a’ is stored.


int count[]=new int[26];


* Now we traverse through string s1 which is lower case of string s.

* char ‘a’ has value 97 in ascii. So we subtract 97 from each alphabet.

* After subtracting we get index where we need to increment the value

* in count array.



* Integer variable max is used to store index of largest ascii

* value of alphabet for example suppose s=”abcdq zsed” then

* max will store 122 i.e ascii value of z.

* This variable is just used for printing formatting of last character

* to appear in output. See below for better understanding


int max=0;

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

int asciiValue=(int)s1.charAt(i);


* Now first we need to check if current character is alphabet or

* any delimiter like character space, commas(,) or period(.)

* If asciiValue is in the range of 97 to 122 (both inclusive),

* then it is alphabet.


if(asciiValue>=97 && asciiValue<=122){





* Suppose current char is ‘c’ then its asciiValue is 99

* So to increment count of c in array count[],

* we subtract 97 from 99 and we get 2 as index where we need to

* increment the value of count[2]++;

* Note:: By default array has values 0 at start.




}// For loop ends.


* Here we just print the array count[] with corresponding character.

* We skip index of array where the values are 0 i.e no occurrences

* of the character.


/*Here we will use the variable max*/



for(int i=0;i<26;i++){


* First we check if count[i] is greater then 0 or no.


if(count[i]>0 && i!=max){

System.out.print(“‘”+(char)(i+97)+”‘:”+count[i]+”, “);

}else if(count[i]>0 && i==max){


* Purpose of max was just to not print comma(,)

* at the end of the final count value!!






}// getLetterFreq() function ends here

public static void StrToTokens(String s){


* Creating ArrayList named tokens.


ArrayList<String> tokens=new ArrayList<String>();


* To get the tokens based on the delimiters like

* space, comma(,) and period(.), we traverse through

* the string using a while loop and runs a for loop inside

* while loop to traverse string s until we get any delimiter.

* So while loop hold current index and for loop ends where there

* is delimiter. See below for better understanding.



* An integer variable named index is declared and initialized to 0.

* The while loop runs until index is less then length of string s.


int index=0;



* The for loop starts from index variable and goes

* upto length of string.


int i;



* Now suppose in for loop current character is any delimiter

* then we do following.


char ch=s.charAt(i);

if(ch==’ ‘ || ch==’,’ || ch==’.’){


* A new string named currentToken is declared.

* It is intialized with using substring of string s.

* substring() function takes two input index’s of string s.

* first index tells from where to start the substring and

* Second index tells where to end(excluding itself.)

* For example string s=”abcdefg” the substring(2,4) will give

* string currentToken=”cd”. Note that charater at index 4 is not

* included.


String currentToken=s.substring(index,i);



* We only add tokens which have length greater then 0.





* Here we increment the index.

* inilially index is 0, but when we get any delimeter

* then index get updated to next position of delimiter.




* For loop is broken and while loop condition is checked.




}//for loop ends here



* If there is no delimiter at end of string.



/*Break the while loop finally*/




* THIS PROCESS REPEATS UNTIL index < s.length().





* Finally we just print the tokens by traversing through

* ArrayList tokens using for loop.


for(int i=0;i<tokens.size();i++){



}//Function StrToTokens() ends here

}//class ends here


Test case 1::

Test case 2::

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.


Let us write that nagging essay.


Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.


Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.


Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.


Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?