2013-02-14 83 views
0

我有這個if語句,當我檢查一個JOptionPane.showMessageDialog(如代碼頂部所示)返回false,但塊仍然執行。我看不到我應該做的不同。

變量:Java boolean if-statement up up

  • C = GregorianCalendar的
  • S =字符串
  • H = HashMap的陣列 - 字符串,日期
  • oldGoods1,2,和3 =包含HashMap - 字符串,日期

代碼:

JOptionPane.showMessageDialog(null, c.after((Date) h[counter].get(s))); 
if(c.after((Date) h[counter].get(s))); //this line 
{ 
    Calendar today = Calendar.getInstance(); 
    if(today.after((Date) h[counter].get(s))) 
    { 
    GoodsList.removeDate(s, (Date) h[counter].get(s)); 
    } 
    if(!oldGoods1.containsKey(s)) 
    { 
    oldGoods1.put(s, (Date) h[counter].get(s)); 
    } 
    else if(!oldGoods2.containsKey(s)) 
    { 
    oldGoods2.put(s, (Date) h[counter].get(s)); 
    } 
    else if(!oldGoods3.containsKey(s)) 
    { 
    oldGoods3.put(s, (Date) h[counter].get(s)); 
    } 
} 

由於預先 Highace2

+9

你的if語句後面有一個分號。其餘的代碼以塊的形式執行。一個相當普遍的初學者錯誤。 – Perception 2013-02-14 23:47:17

回答

9
if(c.after((Date) h[counter].get(s))); // The `;` is the culprit. Remove it. 

有分號在if statement的結束,這只是終止if statement那裏,以下塊這僅僅是一個局部塊,始終不管執行什麼條件評估爲。

+1

哦,該死的......我一直坐着,在3個小時內找出那個錯誤!無論如何感謝您的幫助。 – Highace2 2013-02-14 23:53:19