-1

I've a problem with the following code:

public String SearchText(String fileName, String First, String Last) {
        String tag = new String();
        String file = FileUtils.readFileToString(new File(fileName));
        tag = StringUtils.substringBetween(file, First, Last);
        return First + tag + Last;
    }

It returns me also a "null" value after the string, for example: if I search text between tags <a> and </a> it returns <a>null</a>. The problem is that "tag" as a null value! The textfile as multiple tags with the same name, each of this including different text: is it possible that the problem is there? And how can I solve it?

  • Sidenote: get rid of the initialization of `tag` in the first line of your method, simply write `String tag;`. There's no use in assigning a value if you overwrite it two lines below. – Joachim Sauer Aug 20 '12 at 15:03
  • There's no here the problem, it doesn't change nothing! – user1595513 Aug 20 '12 at 15:13
  • I know that that was not the problem, that's why I said "Sidenote". It was just a general hint regarding code quality. – Joachim Sauer Aug 20 '12 at 15:14
  • Another sidenote: You should stick to the java conventions and camelCase your variables, makes it easier to read for us. – atamanroman Aug 20 '12 at 16:09

1 Answers1

1

What you say isn't possible. If SearchText returns <a>text</a>null, then that must mean:

First = "<a>";
tag = "text";
Last = "</a>null";

which wouldn't find anything. If StringUtils.substringBetween() can't find a match, it returns null which means SearchText would return <a>null</a>null. With the code above, the only way to get <a>text</a>null is:

First = "<a>text</a>";
Last = "";

So what you say in your question isn't possible. Check the content of all variables again, something is not as you say.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Exactly, and I want to remove "null" but I don't know how. – user1595513 Aug 20 '12 at 15:10
  • In that case, you should show us the code which calls the method above. – Aaron Digulla Aug 20 '12 at 15:17
  • Excuse me, it's First that contains null. The code it's a simple call in the main. – user1595513 Aug 20 '12 at 15:35
  • If `First` contained `null`, the code above would return `nulltext`. There is something wrong with the information that you're giving us. Please make sure that `First`, `Last` and `file` contain what you expect. – Aaron Digulla Aug 20 '12 at 15:52
  • The problem is that "tag" as a null value! The textfile as multiple tags with the same name, each of this including different text: is it possible that the problem is there? – user1595513 Aug 21 '12 at 16:20