import java.awt.*; import javax.swing.*; import java.awt.event.*; public class BinaryGUI extends BinaryGUIDemo { public static void main(String[] args) { new BinaryGUI(); } private JFrame frame; private JButton button; private JTextField text; private JTextField text2; private String decimalnumber = null; public BinaryGUI() { frame = new JFrame("Conversion!"); JLabel label = new JLabel("Binary:"); frame.setLayout(new FlowLayout()); frame.add(label); text = new JTextField(15); frame.add(text); JLabel label2 = new JLabel("Decimal:"); frame.add(label2); text2 = new JTextField(15); frame.add(text2); button = new JButton("Convert"); button.addActionListener(new ButtonListener()); frame.add(button); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void Convert() { String binary = text.getText(); int i; for (i = 0; i < binary.length(); i++) { char select = binary.charAt(binary.length() - i - 1); char number = (char) (select * Math.pow(2, i)); decimalnumber += number; } } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Convert(); if (button.getText().equals("Convert")) { text2.setText(decimalnumber); text.getText(); text2.getText(); } else { } } } } I can run the program but for some reason it shows/ puts random characters instead of the initial value of the binary and I cant seem to run the program decimal back to binary. how can I improve this codes. To make it clear it does not convert binary to decimal, and how will I convert it back decimal to binary and if there is some codes that would help me, would be very appreciated.
char number = (char) ((select-'0') * Math.pow(2, i));but there are better ways of doing this. Java method names begin lower case, btw, as do variable names: technojeeves.com/index.php/aliasjava1/…char. Multiplication does not do what you think it does withcharbecause the characters 1 and 0 do not correspond to theintvalues 1 and 0. As for converting decimal to binary, you haven't even implemented it, and there are plently of explanations online on how to do so, which you can easily find.0converted to acharis not the value "0", it has the value "48". Have a look at the ASCII table