Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[JAVA] This is correct but I don't understand how to make it run. Please help. import java.util.*; import java.io.*; public class CountWordsFile { public static

[JAVA]

This is correct but I don't understand how to make it run. Please help.

import java.util.*; import java.io.*;

public class CountWordsFile {

public static void main(String[] args){

String fileName = args[0];

TreeMap treeMap=new TreeMap();

int wordCount = 0;

try{

Scanner input = new Scanner(new File(fileName));

while (input.hasNextLine()){

String line=input.nextLine();

String[]words=line.split("\\s+|(?=\\p{Punct})|(?<=\\p{Punct})");

wordCount = wordCount+words.length;

for (int i=0;i

if (words[i].trim().length () >0 && words[i].trim().matches("[A-Z|a-z]+")){

String key =words[i].toLowerCase();

if (treeMap.get(key)!=null){

int count =treeMap.get(key);

count++;

treeMap.put(key,count);

}

else{

treeMap.put(key, 1);

}

}

}

}

}

catch (Exception ex){

ex.printStackTrace();

}

Set>entrySet=treeMap.entrySet();

System.out.println(" Total words in the file: "+wordCount);

for (Map.Entryentry: entrySet) {

System.out.println(entry.getValue()+"\t" +entry.getKey());

}

}

}

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

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions