Question: 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

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.

Make sure the output prints out only one integer, other solutions I've tried dont do this.

Heres the starter code:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.ArrayList;

public class MostAnagrams {

public static void main(String[] args) {

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

int max = 0;

try { Scanner scanner = new Scanner(file); while (scanner.hasNext()) { String line = scanner.next(); // read in the next word // Now do something with the word // } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }

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

System.out.println( max);

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!