JAVA PPROGRAM ASAP Please Modify this program ASAP BECAUSE IT IS MY LAB ASSIGNMENT so it passes all the test cases. It does not pass the test cases when I upload it to Hypergrade. Because RIGHT NOW IT PASSES 0 OUT OF 1 TEST CASES. I have provided the failed the test cases and the inputs as a screenshot. The program must pass the test case when uploaded to Hypergrade. import java.util.Scanner; // creating class WordCounter public class WordCounter { public static void main(String[] args) { // creating a scanner object Scanner sc = new Scanner(System.in); // if while loop is true while (true) { // then enter a string System.out.print("Please enter a string or type QUIT to exit:\n"); // Taking string as user input String str = sc.nextLine(); // if user enters quit then ignore the case if (str.equalsIgnoreCase("QUIT")) { break; } // Counting the words present in inputted string and store it in count variable int count = countWords(str); // print rhe number of words in the given string System.out.println("There are " + count + " words in that string.\n"); } sc.close(); } // creating a method countWords for counting the words public static int countWords(String input) { // counting the words which are splitted with space String[] words = input.split(" "); // Return the length of the words return words.length; } } Test Case 1 Please enter a string or type QUIT to exit:\n Hello World!ENTER There are 2 words in that string.\n Please enter a string or type QUIT to exit:\n I have a dream.ENTER There are 4 words in that string.\n Please enter a string or type QUIT to exit:\n Java is great.ENTER There are 3 words in that string.\n Please enter a string or type QUIT to exit:\n quitENTER
import java.util.Scanner;
// creating class WordCounter
public class WordCounter
{
public static void main(String[] args)
{
// creating a scanner object
Scanner sc = new Scanner(System.in);
// if while loop is true
while (true)
{
// then enter a string
System.out.print("Please enter a string or type QUIT to exit:\n");
// Taking string as user input
String str = sc.nextLine();
// if user enters quit then ignore the case
if (str.equalsIgnoreCase("QUIT")) {
break;
}
// Counting the words present in inputted string and store it in count variable
int count = countWords(str);
// print rhe number of words in the given string
System.out.println("There are " + count + " words in that string.\n");
}
sc.close();
}
// creating a method countWords for counting the words
public static int countWords(String input)
{
// counting the words which are splitted with space
String[] words = input.split(" ");
// Return the length of the words
return words.length;
}
}
Test Case 1
Hello World!ENTER
There are 2 words in that string.\n
Please enter a string or type QUIT to exit:\n
I have a dream.ENTER
There are 4 words in that string.\n
Please enter a string or type QUIT to exit:\n
Java is great.ENTER
There are 3 words in that string.\n
Please enter a string or type QUIT to exit:\n
quitENTER
Step by step
Solved in 5 steps with 3 images