0
我遇到了不忽略大小寫的代碼問題。這就是我需要做的按計數和不區分大小寫排序
問題
processInput()
應該打開並解析輸入文件。它應該拋出任何可能的例外並允許使用類來處理它們。
generateStatsFile()
應產生下面描述的XML文件。它也應該拋出任何可能的異常,並允許使用類來處理它們。您可以使用PrintWriter來創建您的XML文件,但您應該研究更好的選項。查看本實驗所附的代碼示例。
TEXT文件分析器
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class TextFileAnalyzer {
private File inputFile;
private File statsXMLFile;
private ArrayList<Word> words;
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
TextFileAnalyzer oneTime = new TextFileAnalyzer();
oneTime.processInput(map);
}
public void processInput(HashMap<String, Integer> map) {
System.out.print("Enter file name: ");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.next();
try {
keyboard = new Scanner(new File(fileName)).useDelimiter("[^a-zA-Z]+");
}
catch (FileNotFoundException fnf) {
System.out.println("The file: '" + fileName + "' does not exist!");
System.exit(0);
}
// has it print line by line
while (keyboard.hasNext()) {`enter code here`
String word = keyboard.next();
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
}
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry);
}
keyboard.close();
}
public void generateStatsFile(HashMap<String, Integer> map) {
//SOMECODE TO EXPORT TO XML
}
}
==============
WORD
import java.io.*;
public class Word implements Comparable {
private String word;
private int usageCnt;
public Word() {
this("??", 0);
}
public Word(String word, int usageCnt) {
}
public void reset(String word, int usageCnt) {
this.setWord(word);
this.usageCnt = usageCnt;
}
// ACCESSORS
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getUsageCnt() {
return usageCnt;
}
public void setUsageCnt(int usageCnt) {
this.usageCnt = usageCnt;
}
// COMPARABLE
@Override
public int compareTo(Object o) {
return usageCnt;
}
}
原文#
Green Eggs and Ham
By Dr. Seuss
I am Sam
Sam I am
That Sam-I-am!
That Sam-I-am!
I do not like
that Sam-I-am!
Do you like green eggs and ham?
I do not like them, Sam-I-am.
I do not like green eggs and ham.
Would you like them here or there?
I would not like them here or there.
I would not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Would you like them in a house?
Would you like them with a mouse?
I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
Would you eat them in a box?
Would you eat them with a fox?
Not in a box.
Not with a fox.
Not in a house.
Not with a mouse.
I would not eat them here or there.
I would not eat them anywhere.
I would not eat green eggs and ham.
I do not like them, Sam-I-am.
Would you? Could you?
In a car?
Eat them! Eat them!
Here they are.
I would not, could not, in a car.
You may like them.
You will see.
You may like them in a tree!
I would not, could not in a tree.
Not in a car! You let me be.
I do not like them in a box.
I do not like them with a fox.
I do not like them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them anywhere.
I do not like green eggs and ham.
I do not like them, Sam-I-am.
A train! A train!
A train! A train!
Could you, would you, on a train?
Not on a train! Not in a tree!
Not in a car! Sam! Let me be!
I would not, could not, in a box.
I could not, would not, with a fox.
I will not eat them with a mouse.
I will not eat them in a house.
I will not eat them here or there.
I will not eat them anywhere.
I do not eat green eggs and ham.
I do not like them, Sam-I-am.
Say!
In the dark? Here in the dark!
Would you, could you, in the dark?
I would not, could not, in the dark.
Would you, could you, in the rain?
I would not, could not, in the rain.
Not in the dark. Not on a train.
Not in a car. Not in a tree.
I do not like them, Sam, you see.
Not in a house. Not in a box.
Not with a mouse. Not with a fox.
I will not eat them here or there.
I do not like them anywhere!
You do not like green eggs and ham?
I do not like them, Sam-I-am.
Could you, would you, with a goat?
I would not, could not, with a goat!
Would you, could you, on a boat?
I could not, would not, on a boat.
I will not, will not, with a goat.
I will not eat them in the rain.
I will not eat them on a train.
Not in the dark! Not in a tree!
Not in a car! You let me be!
I do not like them in a box.
I do not like them with a fox.
I will not eat them in a house.
I do not like them with a mouse.
I do not like them here or there.
I do not like them ANYWHERE!
I do not like green eggs and ham!
I do not like them, Sam-I-am.
You do not like them.
So you say.
Try them! Try them!
And you may.
Try them and you may, I say.
Sam!
If you will let me be, I will try them.
You will see.
<at which point in our story, the heretofore tormented hero cautiously
takes a bite of the "green eggs and ham">
Say!
I like green eggs and ham!
I do! I like them, Sam-I-am!
And I would eat them in a boat.
And I would eat them with a goat...
And I will eat them in the rain.
And in the dark. And on a train.
And in a car. And in a tree.
They are so good, so good, you see!
So I will eat them in a box.
And I will eat them with a fox.
And I will eat them in a house.
And I will eat them with a mouse.
And I will eat them here and there.
Say! I will eat them ANYWHERE!
I do so like
green eggs and ham!
Thank you!
Thank you,
Sam-I-am!
正確的輸出
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TEXT_ANALYZER>
<WORD>
<WORD_TEXT>not</WORD_TEXT>
<USAGE_CNT>84</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>i</WORD_TEXT>
<USAGE_CNT>71</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>them</WORD_TEXT>
<USAGE_CNT>61</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>a</WORD_TEXT>
<USAGE_CNT>60</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>like</WORD_TEXT>
<USAGE_CNT>44</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>in</WORD_TEXT>
<USAGE_CNT>41</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>do</WORD_TEXT>
<USAGE_CNT>37</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>you</WORD_TEXT>
<USAGE_CNT>34</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>and</WORD_TEXT>
<USAGE_CNT>27</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>would</WORD_TEXT>
<USAGE_CNT>26</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>eat</WORD_TEXT>
<USAGE_CNT>25</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>will</WORD_TEXT>
<USAGE_CNT>21</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>with</WORD_TEXT>
<USAGE_CNT>19</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>could</WORD_TEXT>
<USAGE_CNT>14</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>the</WORD_TEXT>
<USAGE_CNT>13</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>sam-i-am</WORD_TEXT>
<USAGE_CNT>13</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>ham</WORD_TEXT>
<USAGE_CNT>13</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>eggs</WORD_TEXT>
<USAGE_CNT>13</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>green</WORD_TEXT>
<USAGE_CNT>12</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>here</WORD_TEXT>
<USAGE_CNT>11</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>train</WORD_TEXT>
<USAGE_CNT>9</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>there</WORD_TEXT>
<USAGE_CNT>9</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>or</WORD_TEXT>
<USAGE_CNT>8</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>mouse</WORD_TEXT>
<USAGE_CNT>8</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>house</WORD_TEXT>
<USAGE_CNT>8</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>anywhere</WORD_TEXT>
<USAGE_CNT>8</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>on</WORD_TEXT>
<USAGE_CNT>7</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>fox</WORD_TEXT>
<USAGE_CNT>7</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>dark</WORD_TEXT>
<USAGE_CNT>7</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>car</WORD_TEXT>
<USAGE_CNT>7</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>box</WORD_TEXT>
<USAGE_CNT>7</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>tree</WORD_TEXT>
<USAGE_CNT>6</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>so</WORD_TEXT>
<USAGE_CNT>5</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>say</WORD_TEXT>
<USAGE_CNT>5</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>sam</WORD_TEXT>
<USAGE_CNT>5</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>try</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>see</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>rain</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>me</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>may</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>let</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>goat</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>be</WORD_TEXT>
<USAGE_CNT>4</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>that</WORD_TEXT>
<USAGE_CNT>3</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>boat</WORD_TEXT>
<USAGE_CNT>3</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>they</WORD_TEXT>
<USAGE_CNT>2</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>thank</WORD_TEXT>
<USAGE_CNT>2</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>good</WORD_TEXT>
<USAGE_CNT>2</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>are</WORD_TEXT>
<USAGE_CNT>2</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>am</WORD_TEXT>
<USAGE_CNT>2</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>which</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>tormented</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>takes</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>story</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>seuss</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>point</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>our</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>of</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>if</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>hero</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>heretofore</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>dr</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>cautiously</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>by</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>bite</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT><at</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
<WORD>
<WORD_TEXT>"green</WORD_TEXT>
<USAGE_CNT>1</USAGE_CNT>
</WORD>
</TEXT_ANALYZER>
什麼正在顯示
Enter file name: green_eggs_and_ham.txt
heretofore=1
do=36
anywhere=6
good=2
Eggs=1
that=1
mouse=8
would=17
ham=12
me=4
let=3
Try=3
Thank=2
you=26
they=1
train=9
cautiously=1
which=1
eggs=12
rain=4
Seuss=1
like=44
in=39
tree=6
them=61
Do=1
goat=4
am=15
Dr=1
Not=19
Here=2
at=1
And=12
Eat=2
try=1
If=1
Sam=18
here=9
A=4
be=4
Could=3
In=2
I=84
box=7
Say=3
hero=1
Would=9
house=8
our=1
fox=7
point=1
not=65
see=4
car=7
are=2
and=15
bite=1
of=1
dark=7
eat=23
takes=1
so=3
on=7
That=2
a=56
green=12
or=8
may=4
will=21
could=11
ANYWHERE=2
say=2
boat=3
They=1
the=13
with=19
Ham=1
By=1
there=9
Let=1
tormented=1
So=2
Green=1
You=8
story=1
嘗試使用較小的輸入和輸出製作一個較小的示例。用這麼多的文字來閱讀你的問題有點困難。它也會使調試更容易。最後,generateStatsFile()與你的問題有關嗎?目前尚未實施。 – Cecilia
有趣。在正確的輸出中,單詞按降序排列,然後按*後退*字母順序排序。 –
您可以手動編碼您的XML輸出或使用一些庫來生成它。 –