2011-02-11 83 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); 
} 
}