ここでは、各行に整数を含むテキストファイルを読んでいます。複数回出現したすべての整数を出力しています。数値書式テキストファイルからの読み込み時の例外
私が見てきたように、ハッシュマップを使用して、キーと数値としての整数値を値として割り当てました。
ここでは、数値書式例外を取得しています。誰もこれで私を助けることができますか?
package fileread;
import java.io.*;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
HashMap<Integer, Integer> lines = new HashMap<Integer, Integer>();
try {
FileInputStream fstream = new FileInputStream("C:/Users/kiran/Desktop/text.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str;
while ((str = br.readLine()) != null) {
Integer intObj = Integer.valueOf(str);
if (lines.containsKey(intObj)) {
int x = 0;
x = lines.get(intObj);
if (x == 2) {
System.out.println(intObj);
}
lines.put(intObj, x++);
} else {
lines.put(intObj, 1);
}
}
in.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
例外メッセージに、整数として解析しようとしたものが表示されます。 – hmjd
'DataInputStream'は必要ですか?それが問題の原因になるのでしょうか? –
@ nicholas.hauschildはい、そのDataInputStreamは不要です。 'Fstream'を' BufferedReader'コンストラクタに直接渡すことができます。 –