JAVA Write a program that takes a commandline argument repre

JAVA

Write a program that takes a command-line argument representing a text file name. The program should read the given text file and print two lists, each in ascending order. One list should be the words that exist only once in the text file. The other list should be the words that appear more than once. Ignore the case of the words when determining whether a word has appeared more than once. Your program may ignore punctuation.

Solution


//CountOccurrenceOfWords.java

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.*;

public class CountOccurrenceOfWords
{
public static void main(String[] file) throws FileNotFoundException, IOException
{
if (file.length == 0)
{
System.out.println(\"Usage: java CountOccurrenceOfWords targetfile\");
System.exit(0);
}

BufferedReader br = null;
br = new BufferedReader(new FileReader(file[0]));
String text = null;
TreeMap<String, Integer> treemap = new TreeMap<String, Integer>();


try
{
while ((text = br.readLine()) != null)
{
String[] line = text.split(\"[ \ \\t\ .,;:!?(){}]\");

for (int count = 0; count < line.length; count++)
{
String key = line[count].toLowerCase();
if (key.length() > 0)
{
if (treemap.get(key) == null)
{
treemap.put(key, 1);
}
else
{
int value = treemap.get(key).intValue();
value++;
treemap.put(key, value);
}
}
  
}

}
Set<Map.Entry<String, Integer>> entrySet = treemap.entrySet();
  
// create an empty array list with an initial capacity
ArrayList<String> list1 = new ArrayList<String>(100);
ArrayList<String> list2 = new ArrayList<String>(100);

for (Map.Entry<String, Integer> entry : entrySet)
{
if(entry.getValue() == 1 ) list1.add(entry.getKey());
else list2.add(entry.getKey());
}

System.out.print(\"Words that appear \" + \"one \"+ \"time are: \");
for (String word : list1)
{
System.out.print(word + \", \");
}

System.out.println(\"\ \");

System.out.print(\"Words that appear more than one time are: \");
for (String word : list2)
{
System.out.print(word + \", \");
}

System.out.println(\"\ \");
  
}
catch (IOException error)
{
System.out.println(\"Invalid File\");
}
finally
{
br.close();
}
}
}

/*
words.txt
Sophie Sally and Jack were dachshunds
Dachshunds are the best dogs and all dogs
are better than cats


output:

Words that appear one time are: all, best, better, cats, jack, sally, sophie, than, the, were,

Words that appear more than one time are: and, are, dachshunds, dogs,

*/

JAVA Write a program that takes a command-line argument representing a text file name. The program should read the given text file and print two lists, each in
JAVA Write a program that takes a command-line argument representing a text file name. The program should read the given text file and print two lists, each in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site