最大的Arraylist

时间:2013-11-10 19:45:08

标签: java arraylist

我有一个放入数组的txt文件。 txt文件中的数据格式为:

Order #     Date     Name     City     State    Zip Code    Transaction Amount

跨越一行,每个项目代表一列。

然后还有1000多行用答案填写。我需要帮助从交易金额中查找前3个金额。我坚持如何做到这一点。

这是我的代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

public class Rewards {

public static void main(String[] args) throws FileNotFoundException {

    FileReader fin = new FileReader(
            "C:/Users/Jordan/Desktop/Project5Text.txt");
    Scanner src = new Scanner(fin);
    ArrayList<String> lines = new ArrayList<String>();

    Random rand = new Random();

    while (src.hasNext()) {
        String l = src.nextLine();
        System.out.println(l);
        if (!l.equals(""))
            lines.add(l);

    }

    System.out.println();

    String[] randomChoices = new String[1];

    for (int i = 0; i < randomChoices.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices[i] = randomString;
    }

    for (String s : randomChoices)
        System.out.println("Random Winner for $20 gift card is:       " + s);

    String[] randomChoices1 = new String[1];

    for (int i = 0; i < randomChoices1.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices1[i] = randomString;
    }

    for (String s : randomChoices1)
        System.out.println("Random Winner for $40 gift card is:       " + s);

    String[] randomChoices2 = new String[1];

    for (int i = 0; i < randomChoices2.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices2[i] = randomString;
    }

    for (String s : randomChoices2)
        System.out.println("Random Winner for $60 gift card is:       " + s);

    String[] randomChoices3 = new String[1];

    for (int i = 0; i < randomChoices3.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices3[i] = randomString;
    }

    for (String s : randomChoices3)
        System.out.println("Random Winner for $80 gift card is:       " + s);

    String[] randomChoices4 = new String[1];

    for (int i = 0; i < randomChoices4.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices4[i] = randomString;
    }

    for (String s : randomChoices4)
        System.out.println("Random Winner for $100 gift card is:      " + s);



    src.close();
}
}

以下是我正在使用的一些文本文件。

 Order # Date First name Middle Initial Last name Address City State Zip Email Transaction Amount

 1 8/26/2012 Kristina H Chung 947 Martin Ave. Muncie CA 46489 khchung@business.com $593

 2 11/16/2012 Paige H Chen 15 MainWay Rd. Dallas HI 47281 phchen@business.com $516

 3 11/10/2012 Sherri E Melton 808 Washington Way Brazil CA 47880 semelton@business.com $80

 4 9/20/2012 Gretchen I Hill 56 Washington Dr. Atlanta FL 47215 gihill@business.com $989

 5 3/11/2012 Karen U Puckett 652 Maplewood Ct. Brazil FL 46627 kupuckett@business.com $826

 6 7/4/2012 Patrick O Song 679 MainWay Rd. Lafayette GA 47161 posong@business.com $652

1 个答案:

答案 0 :(得分:1)

改为创建自定义Order对象,该对象实现Comparable<Integer>。提示:您希望将每个Order与其价格进行比较。

使用文件中的所有信息填充此Order对象。然后,您可以使用Collections.sort()(然后Collections.reverse())来确保前三个条目是您的最高三个值。