2013-10-12 2 views
0

내가 원하는 것은 JInternalFrame 위치에 대한 일반 텍스트 파일에 저장된 정보를로드하고 프레임을 저장 한 상태로 설정하는 것입니다. 어떤 이유로 캡처 그룹과 문자열을 비교할 수 없습니다. 즉 matcher.group("state")이 ("min", "max" 또는 "normal") 문자열과 비교하면 true가 반환되고 matcher.group("vis")도 마찬가지입니다.java.util.regex.Matcher.group과 문자열 비교의 불연속

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

내 코드는 다음과 같습니다

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") 
         + "\""); 
    } 
} 

코드는 항상 else 문에 다시 떨어지고있다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? matcher.group은 문자열을 반환해야합니다. 그렇습니까?

+0

코드를 형식화하십시오. –

+0

내 코드의 형식이 무슨 뜻인지 모르겠다. 그것은 이미 포맷되어 있습니다. 그리고 확실히 중복이 아닙니다. 두 개의 질문이 같은 대답을 가질 수 있다고해서 질문 자체가 중복이라는 것을 의미하지는 않습니다 ... – stoicfury

+2

코드가 모두 한 줄로되어있을 때 코드를 읽는 것이 어렵습니다. 한 문장 = 1 줄이되도록 서식을 지정하십시오. String 값을 비교하는 방법을 알지 못하기 때문에 원하는대로 코드를 기대할 수 없습니다. 이것은 모르는 사이에 복제 된 것입니다. –

답변

1

변경

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

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

에 당신은 Strings을 비교 ==를 사용하지 마십시오. .equals 또는 .equalsIgnoresCase 방법을 사용해야합니다.

2

문자열을 "=="연산자로 비교하면 객체가 비교되어 조건이 false가되고 코드의 else 부분으로 이동합니다. 그래서 "=="연산자 대신에 .equals 또는 .equalsIgnoresCase 메서드를 사용하십시오.