由于崩溃的Integer.parseInt [英] Crashing due to Integer.parseInt
问题描述
我试图从已在另一个活动
生成一个文本文件中导入文本。生成的文本文件由一个字符串
的ArrayList
只包含数字和由Android产生的其他随机文本。当我输入我使用该文件中的文本的BufferedReader
和的readLine()
来获取每个新进数量一个整数
的ArrayList
。我是从文本文件中删除任何非数值和在其他活动中产生的号码都被一个\\ n分手了。
这是我面临的问题是,当它加载活动
Android的崩溃。我已经收窄的原因降至的Integer.parseInt()
。
我的code是如下:
的ArrayList<整数GT;行=新的ArrayList<整数GT;(); @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 档案文件=新的文件(getFilesDir(),test_file.txt); 尝试{
BR的BufferedReader =新的BufferedReader(新的FileReader(文件));
而(br.readLine()!= NULL){
字符串文本=(br.readLine())的replaceAll([^ 0-9] +,)修剪()。
整数=的Integer.parseInt(文本);
lines.add(数);
}
}赶上(IOException异常五){ } TextView的电视=(的TextView)findViewById(R.id.helptext); INT最大= 0,最小= 100;
双总= 0;
的for(int i = 0; I< lines.size();我++){
INT数= lines.get(I)
最大= Math.max(最大,数量);
分= Math.min(分钟,数);
总+ =数;
} tv.setText(最大=+ MAX +最小=+分+总=
+总);
问题:
-
当你做
的replaceAll([^ 0-9] +,)
您可以用空字符串引起的Integer.parseInt
抛出NumberFormatException异常
。 -
您跳过每隔一行(你的
,而
循环时所消耗的第一行,第三行等等......)而(br.readLine()!= NULL)//消耗一行
尝试是这样的:
BR的BufferedReader =新的BufferedReader(新的FileReader(文件));
字符串输入;
而((输入= br.readLine())!= NULL){
字符串文本= input.replaceAll([^ 0-9] +,);
如果(!text.isEmpty())
lines.add(的Integer.parseInt(文本));
}
I'm trying to import text from a text file which has been generated in another Activity
. The generated text file is made up of a String
ArrayList
which only contains numbers and the other random text generated by Android. When I import the text from the file I'm using a BufferedReader
and readLine()
to get each new number into an Integer
ArrayList
. I'm removing any non-numerical values from the text file and the numbers that are generated in the other Activity are split up by an "\n".
The problem that I'm facing is that Android crashes when it loads the Activity
. I've narrowed the cause down to Integer.parseInt()
.
My code is below:
ArrayList<Integer> lines = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File file = new File(getFilesDir(), "test_file.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.readLine() != null) {
String text = (br.readLine()).replaceAll("[^0-9]+","").trim();
Integer number = Integer.parseInt(text);
lines.add(number);
}
} catch (IOException e) {
}
TextView tv = (TextView) findViewById(R.id.helptext);
int max = 0, min = 100;
double total = 0;
for (int i = 0; i < lines.size(); i++) {
int number = lines.get(i);
max = Math.max(max, number);
min = Math.min(min, number);
total += number;
}
tv.setText("max = " + max + " min = " + min + " total = "
+ total);
Problems:
When you do
replaceAll("[^0-9]+","")
you can end up with an empty string causingInteger.parseInt
to throw anNumberFormatException
.You are skipping every other line (your
while
loop condition consumes the first line, the third line and so on...)while (br.readLine() != null) // consumes one line
Try something like this:
BufferedReader br = new BufferedReader(new FileReader(file));
String input;
while ((input = br.readLine()) != null) {
String text = input.replaceAll("[^0-9]+","");
if (!text.isEmpty())
lines.add(Integer.parseInt(text));
}
这篇关于由于崩溃的Integer.parseInt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!