Home › Forums › Computers, Games and Technology › Java Time
This topic contains 6 replies, has 4 voices, and was last updated by Rig 3 years, 10 months ago.
- AuthorPosts
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.Example to convert a binary to an integer in Java:
int foo = Integer.parseInt(“1001”, 2);
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.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.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!
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.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.
- AuthorPosts
You must be logged in to reply to this topic.

921526
921524
919244
916783
915526
915524
915354
915129
914037
909862
908811
908810
908500
908465
908464
908300
907963
907895
907477
902002
901301
901106
901105
901104
901024
901017
900393
900392
900391
900390
899038
898980
896844
896798
896797
895983
895850
895848
893740
893036
891671
891670
891336
891017
890865
889894
889741
889058
888157
887960
887768
886321
886306
885519
884948
883951
881340
881339
880491
878671
878351
877678