在arraylist中查找最大值

时间:2013-11-10 21:04:44

标签: java arrays arraylist max

我有一个放入数组的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);

    Object obj = Collections.max(lines);

    System.out.println();
    System.out.println("Highest Transaction Amount is:");
    System.out.println(obj);

    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 :(得分:0)

该行

Object obj = Collections.max(lines);

只是按字母顺序对所有行进行排序。这是因为linesArrayList的{​​{1}},String的作用是根据集合的自然顺序对集合进行排序,并按字母顺序检索最高行。 / p>

您需要解析每行的交易金额,并将每个值添加到Collections.max s(或Integer s的数组中,具体取决于您的具体需求。)

我会替换

Double

ArrayList<String> lines = new ArrayList<String>();

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

同样,最后一行将是:

ArrayList<String> lines = new ArrayList<String>();
ArrayList<Integer> amounts = new ArrayList<Integer>();

while (src.hasNext()) {
    String l = src.nextLine();
    System.out.println(l);
    if (!l.equals("")){
        lines.add(l);
        String amount = l.substring(l.lastIndexOf('$') + 1);
        amounts.add(Integer.parseInt(amount));
    }
}