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
5
  • 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. Commented Oct 26, 2011 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. Commented Oct 26, 2011 at 16:15
  • Well, what throws the NPE? -- additionally, the guard <= 2 is incorrect. It must be >= 2 to be useful. Commented Oct 26, 2011 at 16:15
  • i used the s.split("E").length <= 2 because it could happen that the values is "S01E01 Gulasch" Commented Oct 26, 2011 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. Commented Oct 26, 2011 at 16:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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]);

Comments

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;

1 Comment

This won't produce the correct result because leading 0s will not be output: 00 as an integer is 0, +1 = 1, String.valueOf("1") == 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.