Java: Reads a text and prints two words
Java Basic: Exercise-227 with Solution
Write a Java program that reads a text (only alphabetical characters and spaces) and prints two words. The first one is the word which is frequently used in the text. The second one is the word with the most letters.
Note: A word is a sequence of letters which is separated by the spaces.
Input:
A text is given in a line with following condition:
a. The number of letters in the text is less than or equal to 1000.
b. The number of letters in a word is less than or equal to 32.
c. There is only one word which is arise most frequently in given text.
d. There is only one word which has the maximum number of letters in given text.
Input text: Thank you for your comment and your participation.
Output: your participation.
Visual Presentation:
Sample Solution:
Java Code:
// Importing the Scanner class for user input
import java.util.Scanner;
// Main class named "Main"
class Main {
// Main method to execute the program
public static void main(String args[]) {
// Creating a Scanner object for user input
Scanner sc = new Scanner(System.in);
// Reading a line of text and splitting it into an array of strings
String strs[] = sc.nextLine().split(" ");
// Variables to track the maximum length and frequency
int max_Length = 0;
int indexL = 0;
int max_Frequency = 0;
int indexF = 0;
// Prompting the user to input a text in a line
System.out.println("Input a text in a line:");
// Loop to iterate through the array of strings
for (int i = 0; i < strs.length; i++) {
// Checking and updating the maximum length
if (max_Length < strs[i].length()) {
indexL = i;
max_Length = strs[i].length();
}
// Counting the frequency of the current string
int ctr = 0;
for (int j = i; j < strs.length; j++) {
if (strs[i].equals(strs[j])) {
ctr++;
}
}
// Checking and updating the maximum frequency
if (max_Frequency < ctr) {
indexF = i;
max_Frequency = ctr;
}
}
// Prompting the user with the most frequent text and the word with the maximum number of letters
System.out.println("Most frequent text and the word which has the maximum number of letters:");
System.out.println(strs[indexF] + " " + strs[indexL]);
}
}
Sample Output:
Thank you for your comment and your participation. Input a text in a line: Most frequent text and the word which has the maximum number of letters: your participation.
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous:Write a Java program to print mode values from a given a sequence of integers. The mode value is the element which occurs most frequently. If there are several mode values, print them in ascending order.
Next: Write a Java program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/basic/java-basic-exercise-227.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics