繁体   English   中英

如何将文本文件中的String元素添加到ArrayList中?

[英]How do I add String elements from a text file into an ArrayList?

文本文件包含用逗号分隔的数字(例如458.58、1598.45 ...)。

我想将文本文件中的整行添加到ArrayList中。

到目前为止,这是我的代码:

// to calculate final output
ArrayList<String> weeklySales = new ArrayList<String>(7);

// week 1 sales
while(file.hasNext()) {
    weeklySales.add(file.nextLine());
    System.out.println("I ran!");
}

System.out.println(weeklySales);

编辑:对不起,我的问题不清楚。 我的问题是运行此代码后,它将整个文本文件的所有元素添加到我的数组中,但是我只需要向其自己的单个ArrayList添加1行即可。 因此,总的来说,我将拥有与文件中的文本行一样多的数组列表。

这是代码,可以使用:

String line = "458.58, 1598.45";
String array[] = line.split(", ");

从String更改为double可以通过以下方式进行:

Double.valueOf(array[i]);

可以使用以下方法将数组更改为列表:

ArrayList<String> list = new ArrayList<>();
list.addAll(Arrays.asList(array));

我终于让它工作了。 我意识到,使用单行和从文本文件中分离单个数字的最便捷方法是使用StringTokenizer。 这是完整的代码,您需要的只是文本文件,该文件名为“ SalesData.txt”,而我的文件包括以下三行:

1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36 2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36

该程序输出以下内容:
-每周总销量
-每周平均
-所有星期的总销量
-每周平均
-销量最高的一周
-销量最低的一周

DecimalFormat formatter = new DecimalFormat("#0.00");

// create file object
File salesData = new File("SalesData.txt");

// open file
Scanner file  = new Scanner(salesData);

// declare 2 dimensional array 
double[][] weeklySales = new double[3][7];

// loop to initialize token
int row = 0;
while(file.hasNext()) {
// initialize token
String line = file.nextLine();
StringTokenizer tokens = new StringTokenizer(line, ",");

// fill columns and rows
int col = 0;
while(tokens.hasMoreTokens()) {
        // convert to double and assign to token
    weeklySales[row][col] = Double.parseDouble(tokens.nextToken());
    // move up 1 column
    col++;
    }
// move down 1 row
row++;
}

// calculate weekly sales
double week1Sales = 0, week2Sales = 0, week3Sales = 0;
for(int i = 0; i < 3; i++) {
    for(int j = 0; j < 5; j++) {
        double daily = weeklySales[i][j];
        if(i == 0)
            week1Sales += daily;
        else if(i == 1)
            week2Sales += daily;
        else if(i == 2)
            week3Sales += daily;
    }
}
// week 1 sales
System.out.println("Week 1 total sales: $" +formatter.format(week1Sales));
// week 2 sales
System.out.println("Week 1 total sales: $" +formatter.format(week2Sales));
// week 3 sales
System.out.println("Week 1 total sales: $" +formatter.format(week3Sales));

// average daily for week 1
System.out.println("Daily average for week 1: $" +formatter.format(week1Sales / 7));

// average daily for week 2
System.out.println("Daily average for week 2: $" +formatter.format(week2Sales / 7));

// average daily for week 3
System.out.println("Daily average for week 3: $" +formatter.format(week3Sales / 7));

// total for all weeks
double weeklyTotal = week1Sales + week2Sales + week3Sales;

System.out.println("Total sales of all the weeks: $" +formatter.format(weeklyTotal));

// average weekly sales
System.out.println("Average weekly total: $" + formatter.format(weeklyTotal / 3));

// week number with highest sales
if(week1Sales > week2Sales)
    if(week1Sales > week3Sales)
        System.out.println("The week with the highest sales is week 1.");
    else
        System.out.println("The week with the highest sales is week 3.");
else if(week2Sales > week3Sales)
    if(week2Sales > week1Sales)
        System.out.println("The week with the highest sales is week 2.");
    else
        System.out.println("The week with the highest sales is week 1.");
else
    System.out.println("The week with the highest sales is week 3.");

// week number with the lowest
if(!(week1Sales > week2Sales))
    if(!(week1Sales > week3Sales))
        System.out.println("The week with the lowest sales is week 1.");
    else
        System.out.println("The week with the lowest sales is week 3.");
else if(!(week2Sales > week3Sales))
    if(!(week2Sales > week1Sales))
        System.out.println("The week with the lowest sales is week 2.");
    else
        System.out.println("The week with the lowest sales is week 1.");
else
    System.out.println("The week with the lowest sales is week 3.");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM