Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program called MostAnagrams to find the word with the most anagrams.The input to your program is a single line containing a single positive

Write a program called MostAnagrams to find the word with the most anagrams.The input to your program is a single line containing a single positive integer n, which is the maximum number of lines to read from words.txt. Your program should print a single integer, the maximum number of anagrams of the words in the first n lines of words.txt. (if n is greater than the number of lines in words.txt, then the output is the same as for n equal to the number of lines in words.txt.) For this problem, you may find the String method toCharArray(), the String constructor that takes a character array argument, and the method java.util.Arrays.sort() useful. You may also use the Collections.sort() method. Use the starter code below. No hashmaps. Starter Code:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.Collections;

public class MostAnagrams {

public static void main(String[] args) {

File file = new File("../resource/asnlib/publicdata/words.txt");

Scanner input = new Scanner(System.in);

int maxWords = input.nextInt();

// the maximum number of lines to read int n=0;

// for counting the number of words read

try {

Scanner scanner = new Scanner(file);

while (scanner.hasNext()) {

String line = scanner.next();

// read in the next word

++n;

if(n >= maxWords)

break;

// Now do something with the word

//

}

scanner.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

// compute max, the maximum number of analgrams for any word

int max = 0;

System.out.println( max);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions

Question

What is the environment we are trying to create?

Answered: 1 week ago

Question

How can we visually describe our goals?

Answered: 1 week ago