2014-10-30 80 views
0
private static void ParseInput(String str) 
    { 
     char c ; 
     Integing item1; 
     boolean result; 
     int n =str.length(); 
     Stack StItem = new Stack(n); 
     Queue QItem = new Queue(n); 
     for (int i=0; i<n; i++){ 
      c = str.charAt(i); 
      if ((c == '(') || (c=='[') || (c=='{')){ 
       Integing item = new Integing(i,c); 
       StItem.push(item); 
      }else if((c==')') || (c==']') || (c=='}')){ 
       item1 = StItem.pop();    
       result = item1.getChar().class ==(c.class); //where i do the check 
       if (result){      
        Pair pair1 = new Pair(item1.getNumber(),i); 
        QItem.put(pair1); 
       }else{ 
        System.out.println("Syntax error"); 
        System.exit(-1); 
       } 
      } 
     }   
    } 



public class Integing 
{ 
    private int num; 
    private char par; 

    public Integing(int numb,char paren) 
    { 
     num = numb; 
     par = paren; 
    } 

    public char getChar(){ 
     return this.par; //The method i use to get the exact type 
    } 

    public int getNumber(){ 
     return this.num; 
    } 

} 

我想檢查物品1和C都是相同type.In其實我已經使用的方法,getchar函數()來 採取正是我想要的檢查。我在互聯網上找到方法getClass或class.When我 使用getClass我有解引用錯誤,當我使用類我有這些錯誤。我怎麼可以看到,如果他們是同一類型

[1]:http://i.stack.imgur.com/1kQZg.png

+0

請將您的錯誤粘貼爲文本,而不是圖片。無論如何,c和item1.par都是字符,所以它們怎麼可能是不同類型的? – 2014-10-30 16:59:55

+0

看看[這裏](http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html)。你不能在char上調用getClass,因爲char是一個原始類型。再一次,這裏無用。 – 2014-10-30 17:06:45

+0

我的任務是說item1.par是一個左括號,因爲它是從StItem中提取的,所以我應該檢查它是否與c.Thanks是同一類型的btw感謝:) – 2014-10-30 17:10:08

回答

0

我想你要找的是什麼的instanceof,格式如下所示:

Boolean isChar = item instanceof Character; 

下面是關於使用一些文檔:http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm

我儘管你正在嘗試做什麼,但有點困惑 - 雖然instanceof實現了你使用.class的功能,但我無法看到它在代碼的上下文中對你有什麼幫助。

+0

感謝您的鏈接,比較真的不需要在我的代碼畢竟。雖然,你的意見可能會幫助其他人的字符比較,因爲它是一個原始類和.getClass不工作! :) – 2014-11-05 17:15:00

相關問題