2014-10-30 82 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

相关问题