Question: JAVA Swing ---> Binary-to-Decimal converter. How to display the decimal result/exception throw to the 2nd jTextField? The user inputs a Binary number into the 1st

JAVA Swing ---> Binary-to-Decimal converter. How to display the decimal result/exception throw to the 2nd jTextField?

The user inputs a Binary number into the 1st jTextField. Then after clicking the "Convert" button the program should output that number in its Decimal form. Or if there are any illegal characters (anything but 0 or 1) it will display an error message within the jTextField instead.

I already have all of the methods correct and I know they're responding to the "Convert" button correctly because when you input something like "0101b" into the "Binary" jTextField it throws the correct error.

My issue is that I don't know how to go about making the result/error display itself within the second jTextField. I'm using a driver method that contains the main method. I know it has something to do with setText(); but I can't get it myself.

The issue should be within the actionPerformed class. Everything else *should* be fine.

CODE:

import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class second extends JFrame implements ActionListener{ private JLabel enterString1, enterString2; private JTextField string1, string2; private JButton click; private String storeString1 = ""; public second(){ setLayout(null); setSize(450,180); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); enterString1 = new JLabel("Binary: "); enterString2 = new JLabel("Decimal: "); click = new JButton("Convert"); string1 = new JTextField(); string2 = new JTextField(); enterString1.setBounds(90, 25, 120, 30); enterString2.setBounds(90, 60, 120, 30); string1.setBounds(210, 25, 200, 25); string2.setBounds(210, 60, 200, 25); click.setBounds(17,105,400,30); click.addActionListener(this); add(click); add(string1); add(string2); add(enterString1); add(enterString2); } public static int parseBinary(String binary) throws NumberFormatException { if (!isBinary(binary)) { throw new NumberFormatException("Invalid Format for a Binary String - Illegal character: " + illegal(binary)); } int power = 0; int decimal = 0; for (int i = binary.length() - 1; i >= 0; i--) { if (binary.charAt(i) == '1') { decimal += Math.pow(2, power); } power++; } return decimal; }

public static boolean isBinary(String binary) { for (char ch : binary.toCharArray()) { if (ch != '1' && ch != '0') { return false; } } return true; }

public static char illegal(String iChar) { char test = 0; char arr[] = iChar.toCharArray(); for(char cha : arr) { if (cha != '1' && cha != '0') { test = cha; } } return test; } public void actionPerformed(ActionEvent e) { if(e.getSource() == click) // if the button is clicked, do the following: { storeString1 = string1.getText(); parseBinary(storeString1); isBinary(storeString1); illegal(storeString1); } } }

MAIN METHOD:

public class driver { public static void main(String[] args) { second s = new second(); s.setVisible(true); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!