gpt4 book ai didi

java - 尝试将文本文件读入二维数组并打印到屏幕

转载 作者:行者123 更新时间:2023-12-01 09:14:51 25 4
gpt4 key购买 nike

我试图从文件中读取数据并加载到二维数组中,然后将其打印到屏幕上,但我得到了错误的输出:

输出:

Analysis report of the temperatre reading for the past 10 days 
The code throws an exception
The code throws an exception
The code throws an exception
The code throws an exception
The code throws an exception
The code throws an exception
The code throws an exception
The code throws an exception
The code throws an exception

我创建了一个名为Temperature.txt 的数据文件。数据文件包含过去 10 天的高温和低温,格式为(低)| (高):

+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| Day | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
| Temperature | 30|32 | 29|30 | 25|28 | 25|29 | 27|31 | 28|32 | 26|30 | 24|32 | 24|41 | 27|32 |
+-------------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

下面是我的代码:

import java.util.StringTokenizer;
import java.io.*;

public class Temperature {


public static void main(String[] args) {

StringTokenizer tokenizer;
String line;
String file="temperature.txt";
int[][] temp=new int[10][2];
int sumHigh, sumLow;
FileReader fr=null;
BufferedReader br=null;

try
{
fr=new FileReader(file);
br=new BufferedReader(fr);

line=br.readLine();
System.out.println("Analysis report of the temperature reading for the past 10 days " + line);


for(int row=0; row<temp.length; row++)
{
tokenizer=new StringTokenizer(line, "|");

if(row != 0)
{
try
{

if(row % 2 == 0)
{
sumHigh = Integer.parseInt(tokenizer.nextToken());
temp[row][1]=Integer.parseInt(tokenizer.nextToken());

}
else if (row % 2 != 0)
{

sumLow = Integer.parseInt(tokenizer.nextToken());
temp[row][0]=Integer.parseInt(tokenizer.nextToken());

}
}
catch(Exception e)
{
System.out.println("The code throws an exception");
}
}

}

br.close();

}

catch(FileNotFoundException e)
{
System.out.println("The file " + file + " was not found");
}
catch(IOException e)
{
System.out.println("Reading error");
}
catch(NumberFormatException e)
{
System.out.println("Parsing error");
}
finally
{
if(fr != null)
{
try
{
fr.close();
}
catch(IOException e)
{
System.out.println("Reading error");
}
}
}


}

}

最佳答案

我会将解析温度的逻辑移出到一个单独的函数中。像这样:

public class Temperature {
public static class MinMax {
private int min;
private int max;

public MinMax(int min, int max) {
this.min = min;
this.max = max;
}

public int getMin() {
return this.min;
}

public int getMax() {
return this.max;
}
}

private static List<MinMax> getTemperatures(String line) {
List<MinMax> minMaxList = new ArrayList<>(10);

StringTokenizer tokenizer = new StringTokenizer(line, "|");
System.out.println(tokenizer.nextToken().trim()); //prints out Temperature
while(tokenizer.hasMoreTokens()) {
int minimum = Integer.valueOf(tokenizer.nextToken().trim());
int maximum = Integer.valueOf(tokenizer.nextToken().trim());
minMaxList.add(new MinMax(minimum, maximum));
System.out.println("Day " + (minMaxList.size()) + ": Minimum: " + minimum + " Maximum: " + maximum);
}

return minMaxList;
}

public static void main(String[] args) {
getTemperatures("| Temperature | 30|32 | 29|30 | 25|28 | 25|29 | 27|31 | 28|32 | 26|30 | 24|32 | 24|41 | 27|32 |");
}
}

我不确定您希望如何将温度存储在二维数组中,因此而是为其创建了一个专用类并返回列表中的所有内容。如果您愿意,您可以将该列表放入数组结构中。

另请注意,我使用了 trim() 来删除数字周围的空白。这可以防止您收到 NumberFormatException。

关于java - 尝试将文本文件读入二维数组并打印到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40632060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com