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

The program returns:

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

What is the problem?

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

2

Each invocation of next() consumes one token. Call it once, and save and then compare with the result. Like,

char ch = charIte.next();
if (ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == '(' 
            || ch == ')' || ch == '*' || ch == '"' || ch == '/') {
    // ...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Each time you do charIte.next() you're asking to read the next token. I think what you should do to ischar ite = charIte.next.chartAt(0) and then use ite in your if statement

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

Info on Scanner

magician
  • 547
  • 4
  • 7