0

In android I am getting the String value from BufferedReader and it is null after reading from file.

intstring = br.readLine();
System.out.println(intstring);
if(intstring != null)
{
      System.out.println("Inside if condition");
      int istring = Integer.parseInt(intstring);
}

My output is

 null
 Inside if condition
 NumberFormatException

Help me please

1 Answers1

1

Your NumberFormatException is happening because your input isn't a number. Maybe it's a blank line, or maybe it has some non-numeric characters. In fact, your output suggests that it's actually the word "null".

You've got some options.

  • You could check that the string contains digits and no other characters before you parse it, for example by using a regular expression and a condition like if (intString.matches("\\d+")).
  • You could catch the NumberFormatException, and do something particular when it happens.
  • You could check whether your string is blank, if you knew that there weren't going to be any non-numeric characters in the input. For this option, you might write if (!intString.equals(""))).
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • In the code its printing as null , if you see the output he provided, so there is no chance of getting non numberic character or etc. – developer Jul 17 '14 at 02:38
  • 3
    So, most likely it's actually the word `"null"`. Four non-numeric characters. – Dawood ibn Kareem Jul 17 '14 at 02:41
  • The input from where he is reading might has characters as "null" . – developer Jul 17 '14 at 02:42
  • 2
    @developer - Is there an echo in the room? He just said that :-) – Stephen C Jul 17 '14 at 02:45
  • The bufferedReader store the value of String as "null" and not null if br.readLine() is empty So try checking (!Stringval.equalisIgnoreCase("null")) instead of (Stringval != null) or (Stringval.equalsIgnoreCase(null)) –  Jul 17 '14 at 03:14