-1

I have String values which return via Java Socket. The String values looks like below

<command valid="true" /> 
    and 
<command name="LoadDocument" executed="true" />

I need to read this Strings and store "valid" and "executed" parts values into Boolean variable. ("valid" and "executed" parts can be true or false). I tried following solution,

String str = "<command valid=\"true\" />"; // stored command in a String variable
boolean bool = Boolean.parseBoolean(str); // pass it as an argument

but bool value is always false. how can I do this?

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
KMS
  • 68
  • 9
  • 2
    What approaches have you tried so far? – tgdavies Dec 14 '20 at 03:54
  • 1
    Did you try [`Boolean.parseBoolean(String s)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html#parseBoolean-java.lang.String-)? *"Parses the string argument as a boolean. The `boolean` returned represents the value `true` if the string argument is not `null` and is equal, ignoring case, to the string `"true"`."* – Andreas Dec 14 '20 at 03:56
  • @Andreas I tried that. but always it gives "false". – KMS Dec 14 '20 at 04:24
  • 2
    @KMS Then the string you pass to `parseBoolean()` is not `"true"`. Since you haven't shown us what you've tried, can't help you figure out what you did wrong, but try printing the value, so you can see what text you're trying to parse. – Andreas Dec 14 '20 at 04:41
  • @Andreas I have stored above command (String command = "";) into the String variable and then pass it as an argument to the parseBoolean method. – KMS Dec 14 '20 at 04:44
  • 1
    @KMS You can't do that. *You* need to parse the "command", in order to extract the value of the `valid` attribute. You can then parse that value to a boolean using `parseBoolean()`. Perhaps if you edit the question and show a bit more of what you're trying to do, and what you've tried, you'll get better help. – Andreas Dec 14 '20 at 04:47

2 Answers2

0

You can do this safely. First, you have to make sure the string is not null and its value is Here, the case is ignored. So, "true" or "TRUE" will return boolean true otherwise false

String str = "true";
String str1 = "ok";
boolean bool = Boolean.parseBoolean(str);
System.out.println(bool);
boolean bool1 = Boolean.parseBoolean(str1);
System.out.println(bool1);

result

true
false
Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
  • I tried this solution, when I pass the above command as an argument to the Boolean.parseBoolean method it always gives "false". – KMS Dec 14 '20 at 04:31
  • Please log or debug what you get the value before parsing into boolean. I think it does not get true value that's why it shows false – Md. Shofiulla Dec 14 '20 at 04:36
  • @KMS *"when I pass the above command"* The `parseBoolean` method doesn't know anything about your "command". It knows how to parse `"true"` and `"false"`. Extracting the attribute value from the "command" is your job. The "command" looks like XML, so maybe use an XML parser for that? – Andreas Dec 14 '20 at 04:42
0

I think you can use regexp regex101.com:

private static final String REGEXP = "<command.+(?:valid|executed)=\"(?<val>[^\"]+)\"\\s*\\/>";
private static final Pattern PATTERN = Pattern.compile(REGEXP);

public static boolean isValidOrExecuted(String str) {
    Matcher matcher = PATTERN.matcher(str);
    boolean matches = matcher.matches();
    return matches && Boolean.parseBoolean(matcher.group("val"));
}

Demo:

System.out.println(isValidOrExecuted("<command valid=\"true\" />"));                            // true
System.out.println(isValidOrExecuted("<command valid=\"false\" />"));                           // false
System.out.println(isValidOrExecuted("<command name=\"LoadDocument\" executed=\"true\" />"));   // true
System.out.println(isValidOrExecuted("<command name=\"LoadDocument\" executed=\"false\" />"));  // false
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • Why not use a proper tool like an XML reader? Additionally, if the OP has a finite set of possible strings (assuming the input is always trusted), `indexOf`/`contains` is way easier to read than regexps (and faster). – terrorrussia-keeps-killing Dec 14 '20 at 06:51
  • Yes, ther're many ways to do this. For this simple example - no need to use addition lib (but it does, if you use it more than only for this example). `indexOf` is usually not good, because you can find a wrong part of the line. – Oleg Cherednik Dec 14 '20 at 07:03
  • This is why I would first ask the OP to tell whether they trust the input and if they have a finite set of possible input strings. BTW, your pattern **also** relies on an assumption of having the finite set of inputs: say, for instance, if the input doc is new-lined in the middle of the tag right before the attribute, thus the input is still a valid XML document. https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags so I would not mind adding a library that does the job right. – terrorrussia-keeps-killing Dec 14 '20 at 07:09
  • No need to start a combine for two spikelets – Oleg Cherednik Dec 14 '20 at 07:12
  • I would say the same for promoting bad habits on programming. – terrorrussia-keeps-killing Dec 14 '20 at 07:14