0

All I want to do is load information I saved to a plain text file about the position of a JInternalFrame 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, matcher.group("state") compared to the string it should be ("min", "max", or "normal") is not returning true, and the same goes with matcher.group("vis").

String fileArrayStr = "WINDOW_LAYOUT:0,0|779x768|max|show"

My code is:

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*" +
    "(?<x>[0-9\\-]+),(?<y>[0-9\\-]+)\\|" +
    "(?<length>\\d+)x(?<height>\\d+)\\|" +
    "(?<state>[minaxorl]+)\\|" +
    "(?<vis>[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")
                        + "\"");
    }
}

The code is always falling back to the else statements. What am I doing wrong? matcher.group is supposed to return a string, is it not?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
stoicfury
  • 128
  • 8
  • Please format your code. – Sotirios Delimanolis Oct 12 '13 at 04:47
  • I don't know what you mean by format my code. It is already formatted. And definitely not a duplicate. Just because two questions may have the same answer does not mean the question itself is a duplicate... – stoicfury Oct 12 '13 at 04:49
  • 2
    It is hard to read code when it is all on one line. Format it so that one statement = 1 line. You don't know how to compare String values and that's why your code isn't expecting as you want it to. This is a duplicate without you knowing it. – Sotirios Delimanolis Oct 12 '13 at 04:50
  • I suppose we differ then, in our philosophy of being a moderator here. But thanks for your help. :) – stoicfury Oct 12 '13 at 04:58

2 Answers2

2

You are comparing the string by "==" operator which will compare the objects so the condition gets false and it goes to the else portion of your code. So in place of "==" operator try to use .equals or .equalsIgnoresCase method.

pnathan
  • 713
  • 3
  • 9
1

Change

if( matcher.group("state") == "min" )

to

if( matcher.group("state").equals("min") )

You don't use == to compare Strings. You should use .equals or .equalsIgnoresCase method.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41