配列に入れた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