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
請將您的錯誤粘貼爲文本,而不是圖片。無論如何,c和item1.par都是字符,所以它們怎麼可能是不同類型的? – 2014-10-30 16:59:55
看看[這裏](http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html)。你不能在char上調用getClass,因爲char是一個原始類型。再一次,這裏無用。 – 2014-10-30 17:06:45
我的任務是說item1.par是一個左括號,因爲它是從StItem中提取的,所以我應該檢查它是否與c.Thanks是同一類型的btw感謝:) – 2014-10-30 17:10:08