-1

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

1 回答 1

1

Order而是创建一个自定义对象,该对象实现Comparable<Integer>. 提示:您需要将每个Order产品与其价格进行比较。

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

于 2013-11-10T19:55:19.927 回答