2015-04-30 98 views
2

我正在索引文本文件,我想按字母順序打印出文本文件中的每個單詞和頁碼。進出口運行與字母排序,雖然一個問題...這裏是我目前有...Java按字母順序排序

public void addWord(String word, int num) { 
    boolean match = false; 
    for (IndexEntry x : this) { 
     String i = x.getWord(); 
     if (i.toUpperCase().equals(word.toUpperCase())) { 
      x.add(num); 
      match = true; 
     } 
    } 
    if (match == false) { 
     IndexEntry entry = new IndexEntry(word); 
     int add = 0; 
     int count = 0; 
     boolean spot = false; 
     while (count < this.size() && !spot) { 
      String str = this.get(count).getWord(); 
      if (str.compareTo(word) > 0) { 
       add = count; 
       spot = true; 
      } 
      count++; 
     } 
     this.add(add, entry); 
     this.get(indexOf(entry)).add(num); 
    } 
} 

和這是輸出....

BLUE[5, 8] 
BLACK[7] 
NEW[11] 
OLD[10] 
RED[4] 
TWO[2] 
FISH[1, 2, 4, 5, 7, 8, 10, 11] 
ONE[1] 
Done. 

這顯然不是按字母順序...任何幫助,將不勝感激。謝謝。

這裏是indexEntry

import java.util.List; 
import java.util.ArrayList; 

public class IndexEntry implements Comparable<IndexEntry> 
{ 
    private String word; 
    private List<Integer> numsList; // contains Integer objects 

    /** 
    * Constructs an IndexEntry for a given word 
    * (converted to upper case); stores the word and 
    * creates an empty ArrayList<Integer> for numsList 
    * @param aWord the word for this entry 
    */ 
    public IndexEntry(String aWord) 
    { 
    word = aWord.toUpperCase(); 
    numsList = new ArrayList<Integer>(); 
    } 

    /** 
    * Returns word of this IndexEntry object 
    * @return this entry's word 
    */ 
    public String getWord() 
    { 
    return word; 
    } 

    /** 
    * Adds num at the end of this IndexEntry's numsList if 
    * num is not already in the list; otherwise makes no changes. 
    */ 
    public void add(int num) 
    { 
    if(numsList.contains(num) == false) 
     numsList.add(num); 
    } 

    /** 
    * Compares this entry for equality to another IndexEntry; 
    * the entries are considered equal if their words are 
    * the same 
    * @param obj the other IndexEntry to be compared 
    * @return true if the words match, otherwise false 
    */ 
    public boolean equals(IndexEntry obj) 
    { 
    if(word.equals(obj.getWord())) 
     return true; 
    return false; 
    } 

    /** 
    * Compares this entry to another IndexEntry 
    * by comparing their words 
    * @param obj the other IndexEntry to be compared 
    * @return negative if 'this' entry smaller, 0 if equal, positive is 'this' larger 
    */ 
    public int compareTo(IndexEntry obj) 
    { 
    return obj.getWord().compareTo(word); 
    } 

    /** 
    * Converts this IndexEntry into a string 
    * @return the String representation of this entry: word and line numbers 
    */ 
    public String toString() 
    { 
    return word + numsList; 
    } 
} 

和documentIndex包含addWord

import java.util.StringTokenizer; 

public class DocumentIndex extends java.util.ArrayList<IndexEntry> 
{ 

    /** 
    * Creates an empty DocumentIndex with the default 
    * initial capacity 
    */ 
    public DocumentIndex() 
    { 
    super(); 
    } 

    /** 
    * Creates an empty DocumentIndex with the capacity 
    * given by the parameter 
    * @param init the initial capacity of the list 
    */ 
    public DocumentIndex(int init) 
    { 
    super(init); 
    } 

/** 
    * If word is in this DocumentIndex and num is in its list, does nothing; 
    * if word is in this DocumentIndex and num is not in its list, adds num 
    * to this word's IndexEntry; otherwise creates a new entry with word and 
    * num and inserts it into this index in order 
    * @param word the word to look for 
    * @param num the line number this word is on 
    */ 

    public void addWord(String word, int num) 
    { 
     boolean match = false; 
     for (IndexEntry x : this){ 
      String i = x.getWord(); 
      if (i.toUpperCase().equals(word.toUpperCase())){ 
       x.add(num); 
       match = true;}} 
     if (match == false){ 
      IndexEntry entry = new IndexEntry(word); 
      int add = 0; 
      int count = 0; 
      boolean spot = false; 
      while (count < this.size() && !spot){ 
       String str = this.get(count).getWord(); 
       if (str.compareTo(word) > 0){ 
        add = count; 
        spot = true;} 
       count++;} 
      this.add(add, entry); 
      this.get(indexOf(entry)).add(num);} 
    } 

    /** 
    * For each word found in str, calls addWord(word, num) 
    * @param str a line of text 
    * @param num the line number for this line of text 
    */ 
    public void addAllWords(String str, int num) 
    { 
    StringTokenizer tokens = new StringTokenizer(str, " .,-;?!"); 
      // " .,-;?!" lists delimeters that separate words 

    while(tokens.hasMoreTokens()) 
    { 
     String word = tokens.nextToken(); 
     addWord(word, num); 
    } 
    } 
} 
+3

不在於它解決了什麼,但'i.toUpperCase()。等於(word.toUpperCase())'可以改寫爲'i.equalsIgnoreCase(字)'。 – Pshemo

+0

@ user3808597請更新您的OP中的代碼,以便來自外部的任何人都需要回答您的問題。 –

回答

1

編輯:您需要while循環之後添加以下行addWord

if (!spot && (count == this.size())){ 
    add = count; 
} 

這正好解決當我試圖在我結束的錯誤。

另外,我覺得下面的版本是寫addWord()方法的更清潔,更高效的方式:

public void addWord(String word, int num) { 
    String upperCaseWord = word.toUpperCase(); 

    for (IndexEntry x : this) { 
     String i = x.getWord(); 
     if (i.equals(upperCaseWord)){ 
      x.add(num); 
      return; 
     } 
    } 

    IndexEntry entry = new IndexEntry(word); 
    entry.add(num); 

    int currSize = this.size();  
    if (currSize == 0) { 
     this.add(entry); 
     return; 
    } 

    int count = 0; 
    while (count < currSize) { 
     String str = this.get(count).getWord(); 
     if (str.compareTo(upperCaseWord) > 0){ 
      break; 
     } 

     count++; 
    } 

    this.add(count, entry); 
} 
+0

這些地方我的結果,仍然不是按字母順序http://pastebin.com/bw2FgBKx – user3808597

+0

你可以發佈你用來打印出結果的代碼嗎? – jithinpt

+0

它是在indexEntry中的toString方法 – user3808597

0

Java的字符串通常根據排序的Unicode代碼點的數字順序,這是不是你想要的東西上。使用Collator來按字母排序。

0

我相信這個問題是當字母DocumentIndex正在形成,它可能不知道在什麼地方新的詞。


例如這裏。

IndexEntry entry = new IndexEntry(word); 
int add = 0; 
int count = 0; 
boolean spot = false; 
while (count < this.size() && !spot) 
{ 
    String str = this.get(count).getWord(); 
    if (str.compareTo(word) > 0) 
    { 
     add = count; 
     spot = true; 
    } 

    count++; 
} 

this.add(add, entry); 
this.get(indexOf(entry)).add(num); 

如果循環結束而沒有找到索引來放置字符串字會發生什麼?您的代碼狀態如果字符串單詞在字典順序上不是字符串小於最終的String str,則字符串單詞將簡單地添加到DocumentIndex的開頭(索引0)。因此,我認爲這會在排序算法中產生一個問題,即在最後添加單詞時在列表的開頭添加單詞。


我相信你需要添加一個條件,以檢查是否需要添加到DocumentIndex結束串詞。

IndexEntry entry = new IndexEntry(word); 
int add = 0; 
int count = 0; 
boolean spot = false; 
while (count < this.size() && !spot) 
{ 
    String str = this.get(count).getWord(); 
    if (str.compareTo(word) > 0) 
    { 
     add = count; 
     spot = true; 
    } 

    count++; 
} 

if (spot) // If the loop "spotted" an index, lets add it to the ArrayList. 
{ 
    this.add(add, entry); 
} 
else // Otherwise, lets add it to the end of the ArrayList. 
{ 
    this.add(entry); 
} 

this.get(indexOf(entry)).add(num);