java - Possible lossy from int to char; Occurs when reading in from a text file -
i trying implement shunting algorithm, after read in character, need determine if operand or operator. input file has multiple lines of infix expressions convert postfix expressions , evaluate. need read each character of each line, evaluate, , proceed next line. error is:
"error: incompatible types: possible lossy conversion int char"
so here portion of have far:
bufferedreader input = new bufferedreader(new filereader("input.txt")); char token; char popop; int popint1; int popint2; int result; string line; char temp = 'a'; // while input file still has line characters while ((line = input.readline()) != null) { // create operator , operand stack operatorstack opstack = new operatorstack(); opstack.push(';'); operandstack intstack = new operandstack(); token = input.read(); // first token if(character.isdigit(token)) { system.out.print(token); intstack.push(token); } else if(token == ')') {........ any appreciated.
there couple issues code:
bufferedreader.read()returns-1if you've reached end of stream. you're not handling this.- you reading text line
linewhen callinput.read()reads first char of next line! either useline.charat(0)or useinput.read().
this might fix problem:
// while there characters consume. for(int ch; (ch = input.read()) != -1;) { // create operator , operand stack operatorstack opstack = new operatorstack(); opstack.push(';'); operandstack intstack = new operandstack(); token = (char)ch; // token if(token == '\r' || token == '\n') // handling line ends. continue; if(character.isdigit(token)) { system.out.print(token); intstack.push(token); } else if(token == ')') {........
Comments
Post a Comment