Integer.parseInt crashing

I am trying to import text from a text file that was generated in another Activity . The generated text file consists of a String ArrayList that contains only numbers and other random text created by Android. When I import text from a file, I use BufferedReader and readLine() to get each new number in an Integer ArrayList . I delete any non-numerical values ​​from the text file, but the numbers that are generated in another Activity are divided by "\ n".

The problem I am facing is that Android crashes when Activity loads. I narrowed down the reason 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); 
+7
source share
4 answers

Problems:

  • When you execute replaceAll("[^0-9]+","") , you can get an empty string that calls Integer.parseInt to throw a NumberFormatException .

  • You skip every other line (your while condition consumes the first line, third line, etc.)

     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)); } 
+10
source

All of the above answers are correct, but they will not help you if, for some reason, the data coming to you is not Integer . for example, the server mistakenly sent you a username instead of userId (must be Integer).

This can happen, so we should always put in checks to prevent it. Otherwise, our application will fail, and it will not be pleasant for the user. Therefore, when converting String to Integer always use a try-catch to prevent application crashes. I use the following code to prevent the application from crashing due to Integer parsing -

 try { Log.d(TAG, Integer.parseInt(string)); } catch (NumberFormatException e) { Log.w(TAG, "Key entered isn't Integer"); } 
+2
source

If you specify numbers as strings, such as "1234" , this will not give any exceptions or errors. But you give any character or special character, then the parse () function will throw an exception. Therefore, please carefully check that some character must pass, so it throws an exception and gets a breakdown.

0
source

Make sure that text is just the numbers in the string, most likely not. You can also try:

 Integer number = Integer.valueOf(text); 

instead:

 Integer number = Integer.parseInt(text); 

Cm:

parseInt () returns a primitive integer type (int) in which the Of value returns java.lang.Integer, which is an object representing an integer. There are circumstances in which you might need an Integer object rather than a primitive type.

Edit: after your comments below, I wrote text every time in a loop, most likely when it throws an error, the log will show that the text variable is empty.

0
source

All Articles