Here is what I already have started Answers to Self-Test Questions 6 Write a pro
ID: 3766474 • Letter: H
Question
Here is what I already have started
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
class KeywordDemo
{
String keywordsFound[];
int kcount[];
String words[];
int lineNumber[];
int numOfWords;
int numOfKeywords;
public KeywordDemo()
{
keywordsFound= new String[100];
kcount=new int[100];
for(int i=0;i<100;i++)
kcount[i]=0;
words=new String[500];
lineNumber=new int[500];
numOfWords=0;
numOfKeywords=0;
}
public void fileRead(String userFile) throws FileNotFoundException
{
File file=new File(userFile);
Scanner readFile=new Scanner(file);
while(readFile.hasNextLine())
{
words[numOfWords]=readFile.next();
numOfWords++;
}
}
public boolean found(String word) {
boolean flag=false;
if(numOfKeywords>0)
{
for(int j=0;j<numOfKeywords;j++)
{
if(word.equals(keywordsFound[j]))
{
flag=true;
break;
}
}
}
return flag;
}
public void sortList() {
String temp;
int t;
for (int i = 0; i < numOfKeywords; i++)
{
for (int j = i + 1; j < numOfKeywords; j++)
{
if (keywordsFound[i].compareTo(keywordsFound[j])>0)
{
temp = keywordsFound[i];
keywordsFound[i] = keywordsFound[j];
keywordsFound[j] = temp;
System.out.println("ok");
t=kcount[i];
kcount[i]=kcount[j];
kcount[j]=t;
}
}
}
}
public void displayKeywordList()
{
for(int j=0;j<numOfKeywords;j++)
{
System.out.println(keywordsFound[j]);
}
}
public void displayListWithOccurence()
{
for(int j=0;j<numOfKeywords;j++)
{
System.out.println(keywordsFound[j]+" "+kcount[j]);
}
}
public void findKeyWords(ArrayList<String> keywords)
{
for(int i=0;i<numOfWords;i++)
{
for(int j=0;j<keywords.size();j++)
{
if(words[i].equals(keywords.get(j)))
{
if(!found(words[i]))
{
keywordsFound[numOfKeywords]=words[i];
kcount[numOfKeywords]=1;
numOfKeywords+=1;
}
else
{
kcount[numOfKeywords]+=1;
numOfKeywords+=1;
}
}
}
}
}
}
class ReadIt {
public static void main(String[] args) throws FileNotFoundException {
KeywordDemo K=new KeywordDemo();
ArrayList<String> keywords= new ArrayList<String>();
String[] OtherList= new String[]{"abstract","assert","boolean","break",
"byte","default","do","double","else","extends", "false","final","finally",
"float","for","goto","if","implements","import","instanceof","int", "interface",
"long","native","new", "null","package","private","protected","public","return",
"short","static","strictfp","super","switch","synchronized","this","throw",
"throws","transient","true","try","void","volatile","while"};
keywords.addAll(Arrays.asList(OtherList));
System.out.println("Enter the filename");
Scanner keyboard=new Scanner(System.in);
String userFile=keyboard.nextLine();
K.fileRead(userFile);
K.findKeyWords(keywords);
K.sortList();
System.out.println(" List of unique keywords found in "+userFile+" in Alphabetical order:");
K.displayKeywordList();
System.out.println(" List of unique keywords in Alphabetical order with number of Occurences");
K.displayListWithOccurence();
// System.out.println(" List of unique keywords in Alphabetical order with line number");
}
}