-2

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

1 回答 1

0

什么线

Object obj = Collections.max(lines);

只是按字母顺序对所有行进行排序。这是因为linesis an ArrayListof Strings 并且Collections.max所做的是根据集合的自然顺序对集合进行排序并按字母顺序检索最高行。

您需要从每一行解析交易金额,并将每个值添加到Integers (或Doubles,取决于您的具体需要)的数组中。

我会替换

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));
    }
}

同样,最后几行将是:

Integer amount= Collections.max(amounts);

System.out.println();
System.out.println("Highest Transaction Amount is:");
System.out.println(amount);
于 2013-11-10T23:21:24.820 回答