private JTextField resultTextField;
resultTextField.setText(" "); ...
private class InputListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Stack<Integer> operandStack = new Stack<Integer>();
Stack<Character> operatorStack = new Stack<Character>();
String input = inputTextField.getText();
StringTokenizer strToken = new StringTokenizer(input, " ", false);
while (strToken.hasMoreTokens())
{
String i = strToken.nextToken();
int operand;
char operator;
try
{
operand = Integer.parseInt(i);
operandStack.push(operand);
}
catch (NumberFormatException nfe)
{
operator = i.charAt(0);
operatorStack.push(operator);
}
}
int result = sum (operandStack, operatorStack);
resultTextField.setText(result);
最後一行顯示爲:「resultTextField.setText(result);」在提供的代碼中,我試圖將'result'變量轉換爲String,但沒有成功。編譯錯誤消息我得到的是:將int轉換爲字符串
PrefixExpression.java:96: error: method setText in class JTextComponent cannot be applied to given types; resultTextField.setText(result); ^ required: String found: int reason: actual argument int cannot be converted to String by method invocation conversion.
我試着在答案在這裏已經提供給其他「int轉換成字符串」問題的幾種方法,但他們沒有工作。如何將'結果'轉換爲字符串?謝謝你的幫助。
告訴我們,您是否嘗試過的方法和如何處理他們的問題了。 – RealSkeptic
'Integer.toString(result)'的問題是什麼? – silentprogrammer
@singhakash'result'是'int',而不是'Integer'。 – RealSkeptic