0

配列に入れたtxtファイルがあります。txt ファイル内のデータには、次のような形式のデータがあります。

Order #     Date     Name     City     State    Zip Code

各アイテムが列を表す行全体。

次に、これらに回答が入力された行がさらに 1000 行あります。これらの行から 5 つの行をランダムに選択したいと考えています。何らかの理由で、最初の行 (注文番号、日付など) のみが出力されます。

これが私のコードです:

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

public class Rewards {

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

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

    while (src.hasNext()) {
        lines.add(src.nextLine());
        System.out.println(src.next());
    }
    System.out.println(lines);
    String[] lineArray = new String[lines.size()];
    lines.toArray(lineArray);

    {

        for (int i = 0; i < 5; i++) {
            String random = (lineArray[new Random().nextInt(lineArray.length)]);
            System.out.println("Random Winner is " +random);
        }

    }

}
}

ここに私が使用しているテキストファイルの一部があります。

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 2012 年 8 月 26 日 Kristina H Chung 947 Martin Ave. Muncie CA 46489 khchung@business.com $593

2 2012 年 11 月 16 日 Paige H Chen 15 MainWay Rd. ダラス HI 47281 phchen@business.com $516

3 2012 年 11 月 10 日 シェリー E メルトン 808 ワシントン ウェイ ブラジル CA 47880 semelton@business.com $80

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

5 2012 年 3 月 11 日 Karen U Puckett 652 Maplewood Ct. ブラジル FL 46627 kupuckett@business.com $826

6 2012 年 7 月 4 日 パトリック O ソング 679 MainWay Rd. ラファイエット GA 47161 posong@business.com $652

4

2 に答える 2

0

これを試して:

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

    String fileName = ("test.txt");
    FileReader fin = new FileReader(fileName);
    Scanner src = new Scanner(fin);
    ArrayList<String> lines = new ArrayList<String>();
    src.useDelimiter(":");
            int nChoices = 3;

    Random rand = new Random();

    while (src.hasNext()) {
        String l = src.nextLine();
        if (!l.equals("")) // So we don't add blank lines
            lines.add(l);
    }
    // System.out.println(lines.size());
    String[] randomChoices = new String[nChoices]; // Number of random choices

    for (int i = 0; i < randomChoices.length; i++) {
                    // Here makes the random choice, starting from line 1
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices[i] = randomString;
    }

    for (String s : randomChoices)
        System.out.println(s);

}
于 2013-11-10T17:56:59.490 に答える