从 int 到 char 可能有损;从文本文件读入时发生
Possible lossy from int to char; Occurs when reading in from a text file
我正在尝试实现分流算法,但在读入一个字符后,我需要确定它是操作数还是运算符。输入文件有多行中缀表达式,我将把它们转换为后缀表达式并求值。所以我需要读取每一行的每个字符,评估,然后继续下一行。我得到的错误是:
"error: incompatible types: possible lossy conversion from int to char"
所以这是我目前所拥有的一部分:
BufferedReader input = new BufferedReader(new FileReader("input.txt"));
char token;
char popOp;
int popInt1;
int popInt2;
int result;
String line;
char temp = 'a';
// While the input file still has a line with characters
while ((line = input.readLine()) != null)
{
// Create an operator and operand stack
operatorStack opStack = new operatorStack();
opStack.push(';');
operandStack intStack = new operandStack();
token = input.read(); // Get the first token
if(Character.isDigit(token))
{
System.out.print(token);
intStack.push(token);
}
else if(token == ')')
{........
感谢任何帮助。
BufferedReader
returns 一个 int
。上面的程序试图将该结果存储到 char
变量中。将其类型转换为 char
应该可以修复它:
token = (char)input.read();
您的代码有几个问题:
BufferedReader.read()
returns -1
如果您已到达流的末尾。你不是在处理这个。
- 您已经将文本行读入
line
,因此当您调用 input.read()
时,它会读取下一行的第一个字符!您可以使用 line.charAt(0)
或只使用 input.read()
.
这可能会解决您的问题:
// While there are characters to consume.
for(int ch; (ch = input.read()) != -1;)
{
// Create an operator and operand stack
operatorStack opStack = new operatorStack();
opStack.push(';');
operandStack intStack = new operandStack();
token = (char)ch; // Get the token
if(token == '\r' || token == '\n') // handling line ends.
continue;
if(Character.isDigit(token))
{
System.out.print(token);
intStack.push(token);
}
else if(token == ')')
{........
我正在尝试实现分流算法,但在读入一个字符后,我需要确定它是操作数还是运算符。输入文件有多行中缀表达式,我将把它们转换为后缀表达式并求值。所以我需要读取每一行的每个字符,评估,然后继续下一行。我得到的错误是:
"error: incompatible types: possible lossy conversion from int to char"
所以这是我目前所拥有的一部分:
BufferedReader input = new BufferedReader(new FileReader("input.txt"));
char token;
char popOp;
int popInt1;
int popInt2;
int result;
String line;
char temp = 'a';
// While the input file still has a line with characters
while ((line = input.readLine()) != null)
{
// Create an operator and operand stack
operatorStack opStack = new operatorStack();
opStack.push(';');
operandStack intStack = new operandStack();
token = input.read(); // Get the first token
if(Character.isDigit(token))
{
System.out.print(token);
intStack.push(token);
}
else if(token == ')')
{........
感谢任何帮助。
BufferedReader
returns 一个 int
。上面的程序试图将该结果存储到 char
变量中。将其类型转换为 char
应该可以修复它:
token = (char)input.read();
您的代码有几个问题:
BufferedReader.read()
returns-1
如果您已到达流的末尾。你不是在处理这个。- 您已经将文本行读入
line
,因此当您调用input.read()
时,它会读取下一行的第一个字符!您可以使用line.charAt(0)
或只使用input.read()
.
这可能会解决您的问题:
// While there are characters to consume.
for(int ch; (ch = input.read()) != -1;)
{
// Create an operator and operand stack
operatorStack opStack = new operatorStack();
opStack.push(';');
operandStack intStack = new operandStack();
token = (char)ch; // Get the token
if(token == '\r' || token == '\n') // handling line ends.
continue;
if(Character.isDigit(token))
{
System.out.print(token);
intStack.push(token);
}
else if(token == ')')
{........