2013-11-23 102 views
0

我編寫了一個程序從文本文件中讀取,每行單詞都是一個單詞。代碼的第一部分查找以字母表的每個字母開頭的最長字並將其存儲在數組中。我希望程序要做的第二部分是爲字母表中的每個字母查找該字母出現次數最多的單詞。查找字母中每個字母出現次數最多的單詞

所以輸出應該是這樣的:

最長的一句話:

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]); 
     } 
    } 
} 

}

+0

試着把你的問題分解成更簡單的東西。要求社區解決整個難題不太可能產生一個答案,而不是問他們哪個難題要在這裏。 –

+0

@ Okuma.Scott對不起,這是我第一次在這裏問一個問題,我只是想盡可能多地包含信息。我認爲我比實際情況更接近答案。 – AlexMTMorgan

回答

0

你可以使用這樣的事情:

// Map with the longest word for each letter 
Map<Character, String> longestWordMap = new HashMap<Character, String>(); 
// Map with the word with highest occurrences of each letter 
Map<Character, String> mostCharsWordMap = new HashMap<Character, String>(); 

while((word = br.readLine()) != null) { { 
    word = word.toLowerCase(); 
    Character beginning = word.charAt(0); 
    String longestWord = longestWordMap.get(beginning); 
    // If the current word is the longest, put the word in the map 
    if (longestWord == null || word.length() > longestWord.length()) { 
      longestWordMap.put(beginning, word); 
    } 
    for (char letter = 'a'; letter <= 'z'; letter++) { 
     String mostCharsWord = mostCharsWordMap.get(Character.valueOf(letter)); 
     if (mostCharsWord == null || 
      characterCount(letter, word) > characterCount(letter, mostCharsWord)) { 
      mostCharsWordMap.put(Character.valueOf(letter), word); 
     } 
    } 
} 

這裏是用來計算一個字母的出現一個詞功能:

public static int characterCount(char letter, String word) { 
    int characterCount = 0; 
    for (char c : word.toCharArray()) { 
     if (c == letter) { 
      characterCount++; 
     } 
    } 
    return characterCount; 
} 
0

有可能是一個更直接的方法來此。所以問題實質上是,你有一串文字,並基於當前正在從流中讀取的單詞,將它與數據存儲中已知最長的已知單詞進行比較。如果它更長,則替換單詞,否則什麼也不做。你的邏輯可能是根據別的東西來取代它,比如詞的字典排序。檢查區分大小寫是您的一項練習。

//大多是僞代碼

public class LongestWord 
{ 
    Map<Character,String> longestWords = new HashMap<Character,String>(); 

    while(wordsStream.hasNext()) 
    {  
     String currentWord = wordStream.next(); 
     String longestWordByLetter = longestWords.get(currentWord.charAt(0)); 
     if(null != longestWordByLetter) 
     { 
      if(longestWordByLetter.size() < currentWord.size()) 
      { 
       longestWords.put(currentWord.charAt(0),currentWord); 
      }//else do nothing  
     }else{ 
       longestWords.put(currentWord.charAt(0),currentWord); 
     } 
    } 

} 
相關問題