如果语句被忽略

时间:2014-06-20 08:13:02

标签: android if-statement

在我的onPause中,我收集问题行和可见按钮计数。在onResume我然后使用它们来恢复用户所在的位置。底部的代码是针对问题而且工作正常。问题在于可见按钮。 If语句被忽略。 Logcat向我显示重新调整的可见按钮的值(在这种情况下)是3但是它被忽略。我尝试过不同的方法,例如尝试将“vis”转换为if语句之前的整数,只有numberformat例外。这个当前代码是为if语句声明字符串的值,但没有用。我希望有人能指出我正确的方向。亲切的问候德里克

    private void restoreVis ()  {       
    int i = 2;
    int j = 3;
    String strI = String.valueOf(i);
    String strJ = String.valueOf(j);        
    String file1 = "vis";
    try {
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                openFileInput(file1)));
        String inputString;
        StringBuffer stringBuffer = new StringBuffer();                
        while ((inputString = inputReader.readLine()) != null) {
            stringBuffer.append(inputString + "\n");
        }
        String vis = stringBuffer.toString();
        Log.i(LOGTAG, vis + " visible buttons");

      if (vis.equals(strI)) {
          eliminateChooser();
          eliminateChooser();
        Log.i(LOGTAG, "equals 2");
    } else if (vis.equals(strJ)) {
          eliminateChooser();
          Log.i(LOGTAG, "equals 3");
    }else{
        Log.i(LOGTAG, "vis equals 4");
    }
    } catch (IOException e) {
        e.printStackTrace();
    }
     Log.i(LOGTAG," end of vis");               
}


    LogCat
     06-20 17:50:07.603: I/ON(18818): 3 visible buttons
      06-20 17:50:07.608: I/ON(18818): vis equals 4
      06-20 17:50:35.513: I/ON(18818): on Pause called
      06-20 17:50:35.513: I/ON(18818): 8 row Id
       06-20 17:50:35.513: I/System.out(18818): Visible children: 4

       private void restoreGameState()  {
    String file = "row";
    try {
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                openFileInput(file)));
        String inputString;
        StringBuffer stringBuffer = new StringBuffer();                
        while ((inputString = inputReader.readLine()) != null) {
            stringBuffer.append(inputString + "\n");
        }
        String row = stringBuffer.toString();
        quest = datasource.findGameState(null, row);
        refreshDisplay();
        Log.i(LOGTAG, row + " value of rowId");


    } catch (IOException e) {
        e.printStackTrace();
    }

}

2 个答案:

答案 0 :(得分:4)

我认为这种行为背后的原因是你要将3\n3进行比较,而这些不相等。从\n移除stringBuffer.append(inputString + "\n");

答案 1 :(得分:0)

我建议你使用类似的东西:

else if (Integer.valueOf(vis).intValue() == j)

代替。整数比较更容易。

相关问题