3

Hy!

I want to parse a String to a Integer. The String is like the format for series: SXXEXXX

The Code should increase the episode.

Like: S01E01 --> S01E02 Also: S01E100 --> S01E101

Code:

String s = episodes.get(episodes.size()-1);
Log.e("DBManager",s);
if (s.split("E").length <= 2) {
    int i = Integer.getInteger(s.split("E")[1].split(" ")[0]); //NullPointerEx
    return s.split("E")[0]+"E"+String.valueOf(i++);
}

Log:

10-26 15:56:34.635: E/DBManager(932): S00E01
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
test123123
  • 911
  • 3
  • 16
  • 33
  • 1
    Why are you splitting something on a space? There's no spaces anywhere. Take the second value of the split ("01", "101"), convert that to a number, add 1, format with correct number of leading zeroes, rejoin. – Dave Newton Oct 26 '11 at 16:11
  • 1
    And why the <= 2 test? You'd better have **at least** two elements, because you're trying to get the second one. – JB Nizet Oct 26 '11 at 16:15
  • Well, *what* throws the NPE? -- additionally, the guard `<= 2` is incorrect. It must be `>= 2` to be useful. –  Oct 26 '11 at 16:15
  • i used the s.split("E").length <= 2 because it could happen that the values is "S01E01 Gulasch" – test123123 Oct 26 '11 at 16:29
  • @test123123 Best to include pertinent info like that in the problem statement. If it *might* be something like that then you need to check to see if it actually *is*, rather than assuming you can split on the " ". Answers based on incomplete info will likely be as incorrect as what caused the problem in the first place. – Dave Newton Oct 26 '11 at 16:38

3 Answers3

2

Integer.getInteger() is not the correct method to use.

You should be using Integer.valueOf()

Integer.getInteger(String s) will return the integer system property with the key s.

The null pointer occurs because this method can't find the property with the key you supply and returns null. Java then tries to unbox this to an int and null pointers.

Jim
  • 22,354
  • 6
  • 52
  • 80
  • 1
    Related: http://stackoverflow.com/questions/3123349/why-does-int-num-integer-getinteger123-throw-nullpointerexception – Toomai Oct 26 '11 at 16:40
1

The null pointer exception is occuring precisely because you leave the leading 0 in the string. If you really need to leave it in there though, the following code works for me:

int i = Integer.parseInt(s.split("E")[1].split(" ")[0]);
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
0

If I understand correctly, you want to split the episode name into the parts, then get the episode number and increment it by one to get the next episode number. Don't call this split method so many times, call it once.

    String episodeName = episodes.get(episodes.size()-1);
    Log.e("DBManager", episodeName);
    string[] splittedName = episodeName.split("E");
    string returnName = "";

    if (splittedName.length == 2) {
       if (splittedName[1].split(" ").length > 1) {
          // do something if there's a episode name too
       } else {
          // gets the episode number
          int i = Integer.valueOf(splittedName[1]);

          // returns next episode full name
          if (i < 9) {     
             returnName = splittedName[0] + "E0" + String.valueOf(i + 1);
         } else {
             returnName = splittedName[0] + "E" + String.valueOf(i + 1); 
         }
      }
   }

   return returnName;
evilone
  • 22,410
  • 7
  • 80
  • 107
  • This won't produce the correct result because leading `0`s will not be output: `00` as an integer is 0, +1 = 1, `String.valueOf("1") == 1`. – Dave Newton Oct 26 '11 at 16:15