![](/img/trans.png)
[英]How do I add lines (read from a .txt) separated by AN ENTER KEY (in .txt) into separate elements of 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.