I have this code and in it there is a function to convert binary to decimal. It sets the text of a JTextArea to the result and appends the result to a different JTextArea. The former works fine but the latter causes the above mentioned exception to appear. This is my code below. Pls help.
JButton numerical = new JButton("BIN->NUM");
numerical.setFont(small);
numerical.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Display.setText(String.valueOf(Integer.parseInt(Display.getText(), 2)));
try {
Memory.append(String.valueOf(Integer.parseInt(Display.getText(), 2)));
Memory.append("\n");
} catch (Exception ie) {
Memory.append(String.valueOf(Integer.parseInt(Display.getText(), 2)));
}
}
});
As you say, the first works fine. At this point
Display.setText(String.valueOf(Integer.parseInt(Display.getText(), 2)));
The input value to parseInt()
is in binary form and the conversion works. However the setText()
replaces that binary value with the decimal equivalent. Then when you attempt
Memory.append(String.valueOf(Integer.parseInt(Display.getText(), 2)));
the number is in decimal format and the second conversion fails because you specified base 2 and it expects the number to be in binary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.