Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be writing a simple Java program that counts the words in a String. A \

ID: 672150 • Letter: Y

Question

You will be writing a simple Java program that counts the words in a String. A "skelton" of this code is provided in the file given in the instructions above. You will see that there are three methods declared in this file with no code provided. You must fill in the appropriate code. Pay close attention to what each method should be doing based on the information given in the comments before the method.

Exercise 1 Sample Output

This is a sample transcript of what your program should do. Text in bold is expected input from the user rather than output from the program.

Enter a string: the quick brown fox jumped
Your string has 5 words in it.

If you provide an empty string, your program should report an error until the user enters a non-empty string:

Enter a string:
ERROR - string must not be empty.

Enter a string: A man, a plan, a canal; Panama
Your string has 7 words in it.

NOTE: As described in the comments in the skeleton file, you may assume that words will have exactly one space between them.

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab07b.java in the same ClosedLab07 folder.

For this exercise, you should extend the code that you wrote in Exercise 1 with a new method. This new method should use the following method header:

    private static String getFirstWord(String input)

It should take a String as input and return the first word of that String, using the definition of a word given in Exercise 1. If the String is empty, it should return the empty string.

Then modify your main program so that after it reports how many words are in your String it also reports what the first word of your input String is.

Exercise 2 Sample Output

This is a sample transcript of what your program should do.

Enter a string: The quick brown fox jumped
Your string has 5 words in it.
The first word is: The

If there is only one word in your String, make sure that your program identifies it correctly:

Enter a string: One
Your string has 1 words in it.
The first word is: One

If the user enters an empty String, the code should report an error as it did in Exercise 1:

Enter a string:
ERROR - string must not be empty.

Enter a string: Four score and seven years ago
Your string has 6 words in it.
The first word is: Four

Extensions

If you still have time remaining, or want some more practice with methods, make a copy of Lab7b.java and call it Lab7c.java. Write a new functiongetWord that uses this method header:

private static String getWord(String input, int n)

This function should act just like getFirstWord did in Exercise 2 above, except that instead of returning the first word, it returns the nth word in the string, with the first word as word 0, the second word as word 1, etc. For example the following code:

String myString = getWord("the quick brown fox jumped",3);

should result in the variable myString taking on the value of "fox". If the requested index is out of bounds your function should return an empty String "".

Modfiy your main loop to use this new function and allow the user to pick an arbitrary word to extract from your input String.

Explanation / Answer


import java.util.Scanner;
import java.util.Vector;

public class WordCount
{
private static String getFirstWord(String input)
{
if((input == null)||input.isEmpty())
return null;
  
// char array to collect char by char the First word
char characters[] = new char[input.length()];
  
// variable to track the chars of the first word
int count = 0;
  
// run through the entire sentence, char by char
// starting from the 1st char in the sentence, collect each
// char into the array 'characters'.
// you can collect till you encounter a space
// when a space is encountered we can stop collecting
// but we will check if a first few characters are spaces
// by checking if count is greater than 0
for (int i=0;i<input.length();i++)
{
if(input.charAt(i)!=' ')
characters[count++]=input.charAt(i);
if(input.charAt(i)==' '&& count > 0)
break;   
}
String FirstWord = new String(characters);
  
return FirstWord;
  
}
  
private static int countWords(String input)
{
if((input == null)||input.isEmpty())
return 0;
Vector<String> words= new Vector();
  
// char array to collect char by char the First word
char characters[] = new char[input.length()];
  
// variable to track the chars of the first word
int count = 0;
  
for (int i=0;i<input.length();i++)
{
int j;
for( j=i;j<input.length();j++)
{
if(input.charAt(j)!=' ')
{
characters[count++]=input.charAt(j);
}
if(input.charAt(j)==' '&& count > 0)
break;   
}
String Word = new String(characters);
words.add(Word);
count = 0; i =j;
}
  
return words.size();   
}
private static String getWord(String input,int index)
{
if((input == null)||input.isEmpty())
return null;
Vector<String> words= new Vector();
  
// char array to collect char by char the First word
char characters[] = new char[input.length()];
  
// variable to track the chars of the first word
int count = 0;
  
for (int i=0;i<input.length();i++)
{
int j;
for( j=i;j<input.length();j++)
{
if(input.charAt(j)!=' ')
{
characters[count++]=input.charAt(j);
}
if(input.charAt(j)==' '&& count > 0)
break;   
}
String Word = new String(characters);
words.add(Word);
count = 0; i =j;
}
  
return words.get(index);
}
public static void main(String args[])
{
System.out.println("Counting Words");
System.out.println("Enter a Sentence: ");
Scanner scan = new Scanner(System.in);
String Sentence = scan.nextLine();
  
  
System.out.println("Entered Sentence : "+Sentence);
  
System.out.println("First word is : " + getFirstWord(Sentence));
  
System.out.println("Number of Words is : " + countWords(Sentence));
  
System.out.println("Word at index : " + getWord(Sentence,1));
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote