Note that there are some explanatory texts on larger screens.

plurals
  1. PODiscontinuity between java.util.regex.Matcher.group and string comparison
    text
    copied!<p>All I want to do is load information I saved to a plain text file about the position of a <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JInternalFrame.html" rel="nofollow">JInternalFrame</a> and set the frame to the state it was saved as. For some reason I'm unable to compare capture groups with strings; that is, <code>matcher.group("state")</code> compared to the string it should be (<code>"min"</code>, <code>"max"</code>, or <code>"normal"</code>) is not returning true, and the same goes with <code>matcher.group("vis")</code>.</p> <pre><code>String fileArrayStr = "WINDOW_LAYOUT:0,0|779x768|max|show" </code></pre> <p>My code is:</p> <pre><code>byte[] fileArray = null; String fileArrayStr = null; try { fileArray = Files.readAllBytes(PathToConfig); } catch (IOException e) { e.printStackTrace(); } try { fileArrayStr = new String(fileArray, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } // checking the value of fileArrayStr here by outputting it // confirms the data is read correctly from the file pattern = Pattern.compile( "WINDOW_LAYOUT:\\s*" + "(?&lt;x&gt;[0-9\\-]+),(?&lt;y&gt;[0-9\\-]+)\\|" + "(?&lt;length&gt;\\d+)x(?&lt;height&gt;\\d+)\\|" + "(?&lt;state&gt;[minaxorl]+)\\|" + "(?&lt;vis&gt;[showide]+)\\s*"); matcher = pattern.matcher(fileArrayStr); if (matcher.find()) { frame.setLocation(Integer.parseInt(matcher.group("x")), Integer.parseInt(matcher.group("y"))); frame.setSize(Integer.parseInt(matcher.group("length")), Integer.parseInt(matcher.group("height"))); DialogMsg("state: " + matcher.group("state") + "\n" + "vis: " + matcher.group("vis")); // the above DialogMsg call (my own function to show a popup dialog) // shows the // data is being read correctly, as the values are "max" and "show" // for state // and vis, respectively. Same with the DialogMsg calls below. if (matcher.group("state") == "min") { try { frame.setIcon(true); } catch (PropertyVetoException e) { e.printStackTrace(); } } else if (matcher.group("state") == "max") { try { frame.setMaximum(true); } catch (PropertyVetoException e) { e.printStackTrace(); } } else { DialogMsg("matcher.group(\"state\") = \"" + matcher.group("state") + "\""); } if (matcher.group("vis") == "show") { frame.setVisible(true); } else if (matcher.group("vis") == "hide") { frame.setVisible(false); } else { DialogMsg("matcher.group(\"vis\") = \"" + matcher.group("vis") + "\""); } } </code></pre> <p>The code is always falling back to the <code>else</code> statements. What am I doing wrong? <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#group%28java.lang.String%29" rel="nofollow">matcher.group</a> is supposed to return a string, is it not?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload