Java Time

Topic by JollyMisanthrope

JollyMisanthrope

This topic contains 6 replies, has 4 voices, and was last updated by Rig  Rig 3 years, 10 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #202096
    +2
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    OK I pretty much finished up my latest project and it works fine so far, I just need to put in one more feature.

    Basically this converts Integers to Binary using a GUI. I type in an integer into the first text field and convert. However there is a second text field that I can put in a binary number, but I haven’t coded it to convert to an integer yet. I’m not sure if I need to create a new private method or just add something to the actionPerformed part.

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    @SuppressWarnings("serial")
    public class BinaryTranslator extends JFrame
    
    implements ActionListener
    {
    
    //private static final long serialVersionUID = 1L;
    private JTextField input;
    private JTextField output;
    private JButton convertButton;
    private JButton clearButton;
    
    public BinaryTranslator()
    {
    super("Decimal to Binary");
    setLayout(new FlowLayout());
    setSize(350, 200);
    
    input = new JTextField(30);
    output = new JTextField(30);
    
    convertButton = new JButton("Convert");
    clearButton = new JButton("Clear");
    
    convertButton.addActionListener(this);
    clearButton.addActionListener(this);
    
    convertButton.setActionCommand("convert");
    clearButton.setActionCommand("clear");
    
    this.add(new JLabel("Decimal"));
    this.add(input);
    this.add(new JLabel("Binary"));
    this.add(output);
    this.add(convertButton);
    this.add(clearButton);
    
    }
    
    private String convertToBinary(String numString)
    {
    int num = Integer.parseInt(numString);
    if(num==0)
    return"";
    
    else if(num % 2==1)
    return convertToBinary(Integer.toString(num/2))+"1";
    
    else
    return convertToBinary(Integer.toString(num/2))+"0";
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
    String s = e.getActionCommand();
    
    if(s.equals("convert"))
    
    {
    String aNumber = input.getText();
    
    output.setText(convertToBinary(aNumber));
    }
    
    if(s.equals("clear"))
    {
    input.setText("");
    output.setText("");
    }
    }
    
    
    }
    
    
    
    public class BinaryTester {
    
    public static void main(String[] args) 
    {
    BinaryTranslator b = new BinaryTranslator();
    
    b.setVisible(true);
    
    }
    }
    

    As of now I get an exception error if I try to convert with binary typed into the bottom text field, which is actually the next part of the coding after getting it to convert both ways. Adding exception handling via a pop-up message.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #202231
    Total Lee
    Total Lee
    Participant
    1573

    Example to convert a binary to an integer in Java:

    int foo = Integer.parseInt(“1001”, 2);

    #202451
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Ok, I still need to know if I have to create a new private method for converting binary string to int, like I did with the convertToBinary.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #202456
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    Ok I’ve changed a few things

    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    @SuppressWarnings("serial")
    public class BinaryTranslator extends JFrame
    
    implements ActionListener
    {
    
    //private static final long serialVersionUID = 1L;
    private JTextField input;
    private JTextField output;
    private JButton convertButton;
    private JButton convertButton2;
    private JButton clearButton;
    
    public BinaryTranslator()
    {
    super("Decimal to Binary");
    setLayout(new FlowLayout());
    setSize(350, 200);
    
    input = new JTextField(30);
    output = new JTextField(30);
    
    convertButton = new JButton("Convert");
    convertButton2 = new JButton ("Convert2");
    clearButton = new JButton("Clear");
    
    convertButton.addActionListener(this);
    convertButton2.addActionListener(this);
    clearButton.addActionListener(this);
    
    convertButton.setActionCommand("convert");
    convertButton2.setActionCommand("Convert2");
    clearButton.setActionCommand("clear");
    
    this.add(new JLabel("Decimal"));
    this.add(input);
    this.add(new JLabel("Binary"));
    this.add(output);
    this.add(convertButton);
    this.add(convertButton2);
    this.add(clearButton);
    
    }
    
    private String convertToBinary(String numString)
    {
    int num = Integer.parseInt(numString);
    if(num==0)
    return"";
    
    else if(num % 2==1)
    return convertToBinary(Integer.toString(num/2))+"1";
    
    else
    return convertToBinary(Integer.toString(num/2))+"0";
    }
    
    @Override
    public void actionPerformed(ActionEvent e) 
    {
    String s = e.getActionCommand();
    
    if(s.equals("convert"))
    
    {
    String aNumber = input.getText();
    
    output.setText(convertToBinary(aNumber));
    }
    
    else if (s.equals("convert2"))
    {
    String base10 = input.getText();
    
    output.setText(Integer.toBinaryString(Integer.valueOf(base10)));
    }
    
    
    if(s.equals("clear"))
    {
    input.setText("");
    output.setText("");
    }
    }
    
    
    }
    

    My convert2 button doesn’t seem to do anything, or at least nothing shows up.

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #202486
    +1
    Atton
    Atton
    Participant

    Here’s what I learned about code…
    Never be p~~~ed off when it will not run.
    You just look online and in books and along the way..that journey is s~~~ you learn to be better at it.

    Keep a good excel file with links to s~~~.
    Data filters are handy to organize the links.

    You forgot the part where you practice like crazy.

    A MGTOW is a man who is not a woman's bitch!

    #203174
    JollyMisanthrope
    JollyMisanthrope
    Participant
    3356

    I appreciate the words of wisdom guys but I would appreciate advice on how to tackle this problem even more. 😉

    The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.
    #207636
    +1
    Rig
    Rig
    Participant
    52

    And what debugger is showing in:
    output.setText(Integer.toBinaryString(Integer.valueOf(base10)));
    And most commonly: is the button name is as it should be?

    I see that you are stating with Java. Practice a lot. 🙂 (as I should too)

    Ok, I still need to know if I have to create a new private method for converting binary string to int, like I did with the convertToBinary.

    In Java it’s better to avoid the ‘API approach’ – don’t try to code everything, use code that come with language. Concentrate on features.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.