0
ファイルからデータを読み込んで2次元配列にロードし、それを画面に印刷しようとしていますが、出力が間違っています:テキストファイルを2D配列に読み込んで画面に印刷しようとしています
出力:
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");
}
}
}
}
}
トークン化: 'tokenizer = new StringTokenizer(line、" | ");' '|'文字で 'space'文字を追加します。おそらく例外は 'NumberFormatException'です。トークンにはこのようなスペースがあります: '1'、 '2'など – rafid059
29 | 30をどのようにint配列に配置しますか? – Jobin