【发布时间】:2013-11-23 13:38:02
【问题描述】:
我编写了一个程序来读取一个文本文件,其中每一行都是一个单词。代码的第一部分查找以字母表中每个字母开头的最长单词并将其存储在一个数组中。我希望程序执行的第二部分是为字母表中的每个字母找到该字母出现次数最多的单词。
所以输出应该是这样的:
最长的单词:
a: anthropomorphologically
b: blepharosphincterectomy
c: cholecystenterorrhaphy
d: dacryocystoblennorrhea
e: epididymodeferentectomy
f: formaldehydesulphoxylate
g: gastroenteroanastomosis
h: hematospectrophotometer
i: indistinguishableness
j: jurisprudentialist
k: keratoconjunctivitis
l: laparocolpohysterotomy
m: macracanthrorhynchiasis
n: naphthylaminesulphonic
o: omnirepresentativeness
p: pathologicopsychological
q: quadratomandibular
r: reticulatocoalescent
s: scientificophilosophical
t: tetraiodophenolphthalein
u: ureterocystanastomosis
v: vagoglossopharyngeal
w: weatherproofness
x: xanthocreatinine
y: yohimbinization
z: zoologicoarchaeologist
字母最多的单词:
a: astragalocalcaneal
b: beblubber
c: chlorococcaceae
d: disdodecahedroid
e: electrotelethermometer
f: giffgaff
g: cuggermugger
h: choledochorrhaphy
i: impossibilification
j: ajaja
k: akiskemikinik
l: allochlorophyll
m: dynamometamorphism
n: nonannouncement
o: choledochoduodenostomy
p: aplopappus
q: equivoque
r: archcorrupter
s: possessionlessness
t: anticonstitutionalist
u: untumultuous
v: overconservative
w: bowwow
x: adnexopexy
y: dacryocystosyringotomy
z: zizz
}
基本上,我需要弄清楚如何做到这一点,所以输出不是与第一个字母相同的字母最多的单词(就像上面的'f' [giffgaff] 不以'f'开头)。我用谷歌搜索/搜索了很多,但没有找到任何帮助。
/**
* @param args first String argument is the
* name of the input text file
*/
public static void main(String [] args) throws IOException {
//instance variable
String[] longestWords = new String[26];
String[] mostCharsWord = new String[26];
String currentLine = null;
int[] numCharacters = new int[26];
//because the while loop in try statement is comparing lengths in order to
//assign words, I must give each element a non-null value
//in this case, length = 0
Arrays.fill(longestWords, "");
Arrays.fill(mostCharsWord, "");
//try block
try(BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
String currentLongestWord;
int index;
int indexer = 0;
int count = 0;
int counter = 0;
while((currentLine=br.readLine()) != null) {
currentLine = currentLine.toLowerCase();
index = currentLine.charAt(0)-'a';
currentLongestWord = longestWords[index];
if(currentLine.length() > currentLongestWord.length()) {
longestWords[index] = currentLine;
}
/**
* this code below is for the "AND" bit, but I know that it's not correct.
* Instead of printing out the word with the most occurrences of each
* letter, it prints out the word with the most occurrences of each letter
* THAT BEGINS WITH THAT LETTER
*/
for(char c : currentLine.toCharArray()) {
if(c == currentLine.charAt(0)) {
count += 1;
}
}
for(String currentMostCharsWord : mostCharsWord) {
indexer += 1;
for(char c : currentLine.toCharArray()) {
for( char d: currentMostCharsWord.toCharArray()) {
if(c==d) {
//hmmm....this would compare every letter, not just the one
//that I'm looking for. booooooo
}
}
}
}
if(count > numCharacters[index]) {
numCharacters[index] = count;
mostCharsWord[index] = currentLine;
}
count = 0;
}
//close file!
br.close();
}
//catch block
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//finally / do anyway statement
finally {
System.out.println("Longest Words: \n");
for(int j = 0; j < 26; j++) {
System.out.printf("%s: %s\n", longestWords[j].charAt(0), longestWords[j]);
}
System.out.println("------------------------------------\n\nWords with most letters: \n");
for(int j = 0; j < 26; j++) {
System.out.printf("%s: %s\n", mostCharsWord[j].charAt(0), mostCharsWord[j]);
}
}
}
}
【问题讨论】:
-
尝试将您的问题分解为更简单的问题。要求社区解决整个谜题比询问他们哪个拼图“在这里”更不可能产生答案。
-
@Okuma.Scott 抱歉,这是我第一次在这里提出问题,我只是想包含尽可能多的信息。我以为我比实际更接近答案。