2011-02-11 88 views
0

例如,如何按字母順序排序文本文件?

,我想這樣的數據: -

 Manufacturer : - Violin 

    Type : - Electric 

    Colour: - Blue 

    Price : $23 



    Manufacturer : - Guitar 

    Type : - Electric 

    Colour: - Red 

    Price : $54 

的樣子: -

 Manufacturer : - Guitar 

    Type : - Electric 

    Colour: - Red 

    Price : $54 



    Manufacturer : - Violin 

    Type : - Electric 

    Colour: - Blue 

    Price : $23 

(注意小提琴的信息是如何而來的吉他下方,因爲按字母順序說到後「 G「uitar。)

需要你的幫助儘快用JAVA代碼.....謝謝你的幫助將非常感謝你。

+0

比較方法,你有什麼這麼遠嗎? – OscarRyz 2011-02-11 15:41:44

+0

嘿@OscarRyz .....其實......我只是一個學生......而且用戶給我的解決方案對我來說是相當難理解的......有很多東西在裏面我沒有明白.....你能不能用一種非常簡單易懂的技術來幫助我......也見過使用「.sort」方法的人們......你能給我一個解決方案,不使用這種方法.....而是使用泡沫或序列排序...謝謝....真的很抱歉的不便... – pranavsharma 2011-02-11 18:47:24

+0

Downvoted,因爲你實際上並沒有期待有一個問題的答案,你只是想找人做你的功課。 – 2011-02-11 19:20:54

回答

1

我的建議是逐行讀取文件並創建「Item」類型的普通POJO(其中Item僅僅是您自己編寫的具有4個實例變量(所有Strings)的類,即製造商,類型,顏色和價格)。

在閱讀文件時,您可以爲文本文件中的每4行創建一個Item實例(例如,您的第一個項目將有製造商=「小提琴」,類型=「電」,顏色=「藍色」和價格=「$ 23」)。

然後你可以只是把每一個項目到一個共同的ArrayList爲您創建它們,一旦你讀完該文件,你可以使用下面的正確排序是:

Collections.sort(itemList, new Comparator<Item>() { 

    @Override 
    public int compare(Item o1, Item o2) { 
     return o1.getManufacturer().compareTo(o2); 
    } 
}); 

其中itemList中是ArrayList<Item>

您的列表現在將進行排序,循環遍歷它,並將每個項目中的數據寫入您的ouptut文件,就像那樣簡單。

編輯根據要求,我會提供更多的細節,但是,儘管最初我說我會按照您的要求使用手動排序,我不會再那樣做了。相反,我會嘗試向你解釋Collections.sort方法,因爲知道如何使用它真的很重要。

所以讓我們開始使用Item類。我只想讓這個類公共的所有實例變量,雖然你也許應該相當有他們的私人與通常的getter和setter方法(注意,我也忽略包和import語句):

public class Item { 
    public String manufacturer;   
    public String type;   
    public String colour;   
    public String price; 

    public Item(String manufacturer, String type, String colour, String price) { 
     this.manufacturer = manufacturer; 
     this.type = type; 
     this.colour = colour; 
     this.price = price; 
    } 
} 

這個類用於存儲我們希望排序的數據。現在讓我們在你的主要方法看看:

public static void main(String[] args) { 
    try { 
     BufferedReader reader = new BufferedReader(new FileReader("inputFile.txt")); 
     String nextLine = reader.readLine(); 
     List<Item> listOfItems = new ArrayList<Item>(); 

     while (nextLine != null) { 
      if (!nextLine.equals("\n") && !nextLine.isEmpty()) { 
       String manufacturer = nextLine; 
       String type = reader.readLine(); 
       String colour = reader.readLine(); 
       String price = reader.readLine(); 

       Item newItem = new Item(manufacturer, type, colour, price); 
       listOfItems.add(newItem); 
      } 
      nextLine = reader.readLine(); 
     } 
     reader.close(); 
     Collections.sort(listOfItems, new Comparator<Item>() { 

      @Override 
      public int compare(Item o1, Item o2) { 
       return o1.manufacturer.compareTo(o2.manufacturer); 
      } 
     });    
     PrintWriter writer = new PrintWriter("outputFile.txt"); 

     for (Item item : listOfItems) { 
      writer.println(item.manufacturer); 
      writer.println(item.type); 
      writer.println(item.colour); 
      writer.println(item.price); 
      writer.println(); 
     } 
     writer.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

好了,所以重要的部分是要注意如何Collections.sort作品。你將這個方法傳遞給一個集合(或者更具體地說,在我們的例子中是一個List/ArrayList),它會爲你列出這個列表中的條目。最大的問題是它如何知道哪個Item對象應該第一個,第二個等等。因爲我們要對製造商進行排序,所以我們需要告訴sort()使用他們的製造商實例變量來比較Item對象。

這是sort()定義的第二個參數。這new Comparator<Item>部分告訴排序,它將排序項目對象。 sort()將使用我們定義的compare()方法來執行此排序。它在排序過程中將項目與其他項目進行比較,compare()方法告訴sort()僅比較被排序的項目的製造商,並僅根據該排序進行排序。

如果有什麼東西還不清楚,請具體問我。我認爲理解這個解決方案對你來說比向你展示如何編寫一個冒泡排序更有用。如果你想只是谷歌「Java泡沫排序」和大量的文章/帖子將彈出。

0

這一切都取決於你如何存儲數據。如果您只是使用原始文本文件,則必須編寫Java代碼來解釋數據,對其進行分類並將結果吐出。或者,您可以嘗試將數據加載到數據庫(如MySQL)中,然後通過Java與其交互。

假設你可以將數據加載到Java不知何故,你可以這樣做:

 
public Instrument { 
    public String manufacturer; 
    public String type; 
    public String color; 
    public BigDecimal price; 

    public Instrument(String manufacturer, String type, String color, BigDecimal price) { 
    this.manufacturer = manufacturer; 
    this.type = type; 
    this.color = color; 
    this.price = price; 
    } 

    public int compareTo(Object obj) 
    Instrument temp = (Instrument)obj; 

    return this.manufacturer.compareTo(temp.manufacturer); 
    } 
} 

Instrument[] instruments = new Instrument[100]; // If you have 100 instruments in your file 

instruments = LoadInstruments("INSRUMENTS.txt"); // Just an example of loading instruments 

Arrays.sort(instruments); 
+0

hey @tim .....你可以用Arrays.sort(工具)代替等效的代碼....謝謝.. – pranavsharma 2011-02-11 18:59:16

2

完整的代碼來解決這個問題將是比較長在這裏的響應,但我可以給你一些方向:

首先創建一個包含有關每個你的對象「小提琴」,「吉他」等信息一類基本上,這個類看起來是這樣的:

public class BlockOfText 
{ 
    public String name; 

    public String block; 
} 

,其中「名稱」是「吉他」和「小提琴」,「塊」是整個塊。 現在從輸入文件中讀取每個塊並將它們保存在一個數組中。使塊對象可排序(我認爲在java中有類似於Comparable的東西),並對塊的數組進行排序。現在將數組的每個條目寫入一個文件中。 :)

+0

只是「可比較的」。 – 2011-02-11 15:42:16

1

您需要創建一個包含屬性的類:

  • 廠商
  • 類型
  • 顏色
  • 價格

然後你從文件中讀取數據並用數據創建一個對象,然後將每個對象添加到一個ArrayList,您可以使用Collections.sort(...)方法。

然後,您的班級可以實施Comparable界面,也可以使用BeanComparator。 Bean比較器鏈接有一個實現Comparable的簡單示例,可以創建一個自定義比較器或使用BeanComparator,因此不需要編寫任何自定義代碼。

2

您必須讀取文件並將每個元素存儲在數據結構中。

然後,您只需定義比較方法,就是這樣。

東西沿着線:

class ReadFile { 
    ... 
    void read(){ 
     List<Product> list = ... 
     while(readAllLines()) { 
     if(line.startWith("*")){ 
     list.add(new Product(line)); 
     } 
     } 
     Collections.sort(list); 
    } 
    ... 
} 

,然後定義在Product

class Product implements Comparable<Product> { 
    public Product(String fromLine) { 
    // take values ... 
    } 
    // attributes 
... 
// etc. 
And then 
public int compareTo(Product other) { 
    return this.name.compareTo(other.name); 
} 
}