Write a java program to find the most repeated word in a string and also print its frequency.
INPUT
are you are
OUTPUT
are: 2
This question can be done by using HashMap or file reader (I suppose) but actually, I haven't learned them yet.
Yet, I managed to write a code that displays the frequency (but not the word)
import java.util.Scanner;
class duplicatewords
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String str=sc.nextLine();
String arr[]=str.split(" ");
int count=1; int checkvalue=0;
for(int i=0;i<arr.length-1;i++)
{
String temp=arr[i];
for(int j=i+1;j<arr.length;j++)
{
String anothertemp=arr[j];
if(temp.equalsIgnoreCase(anothertemp))
count++;
}
if(checkvalue<c)
checkvalue=c;
c=1;
}
System.out.println(checkvalue);
}
}
I want to know how to print the word also without using any map or reader.
I think the program will be a very complicated one but I will understand.
Any help will be greatly appreciated.