2017-10-10 63 views
0
if (charIte.next()=='{' || charIte.next()=='}' 
       || charIte.next()=='[' || charIte.next()==']' 
       || charIte.next()=='(' || charIte.next()==')' 
       || charIte.next()=='*' || charIte.next()=='"' 
       || charIte.next()=='/'){ 
} 

程序返回:異常線程 「main」 java.util.NoSuchElementException中如果statem

Exception in thread "main" java.util.NoSuchElementException at line 
|| charIte.next()=='(' || charIte.next()==')' 

問題是什麼?

+0

問題是你打電話'下一個()'太多次。你需要將它存儲在一個變量中。 – shmosel

回答

2

每次調用next()消耗一個令牌。調用一次,然後保存,然後與結果進行比較。像,

char ch = charIte.next(); 
if (ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == '(' 
      || ch == ')' || ch == '*' || ch == '"' || ch == '/') { 
    // ... 
} 
0

每次你做charIte.next()你要求讀取下一個標記。 我認爲你應該做的是什麼char ite = charIte.next.chartAt(0),然後使用ITE在你的if語句

if (ite next()=='{' || next()=='}' 
|| ite.next()=='[' || ite.next()==']' 
      || ite.next()=='(' || ite.next()==')' 
      || ite.next()=='*' || ite.next()=='"' 
      || ite.next()=='/'){ 

Info on Scanner

相關問題