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

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

ID: 672330 • Letter: 1

Question

1.

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.

Ex.

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.

2.


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.

Ex.

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

Explanation / Answer

Exercise1 code:


import java.util.*;
import java.lang.*;
import java.io.*;

class Countwords
{
   public static void main (String[] args) throws java.lang.Exception
   {
       int count,cnt;
       Scanner in = new Scanner(System.in);
System.out.print("Enter a string:");
String str=in.nextLine();
  
while (str.length() ==0){
System.out.println(" ERROR - string must not be empty. ");

System.out.print("Enter a string:");
str=in.nextLine();
}
  
String words[]=str.split(" ");
count=words.length;
  
System.out.println();
System.out.print("Your string has "+ count +" words in it.");
   }
}

+++++++++++++++++++++++++++++++++++++++++

Exercise2 code:


import java.util.*;
import java.lang.*;
import java.io.*;

class Countwords
{
//private static String getFirstWord(String input);
  
   public static void main (String[] args) throws java.lang.Exception
   {
       int count,cnt;
       Scanner in = new Scanner(System.in);
System.out.print("Enter a string:");
String str=in.nextLine();
  
while (str.length() ==0){
System.out.println(" ERROR - string must not be empty. ");
System.out.print("Enter a string:");
str=in.nextLine();
}
  
String words[]=str.split(" ");
count=words.length;
  
System.out.println();
System.out.println("Your string has "+ count +" words in it.");
String first = getFirstWord(str);
System.out.println("The first word is: "+first);
   }
  
   private static String getFirstWord(String input){
String word[]=input.split(" ");
return word[0];
}
}