Converting a string from a decimal to binary number I want to modify the code be
ID: 3910824 • Letter: C
Question
Converting a string from a decimal to binary number
I want to modify the code below so that a NumberFormatException is thrown by the parseBinary(String binaryString) method (the parameter must stay the same but the return type does not have to be String) rather than be caught within a try-catch block like I have implemented below. The user is to enter a binary string in the first text box labeled binary string, click the convert to decimal button, and then the decimal number should be displayed in the second text box labeled decimal string. If the user enters a number other than 0 or 1 or enters any other letter/special character, the second textbook should display an error message and the invalid character in the decimal sring text box. It should also display a similar error when the biary string is longer than 32 characters. I found this easy to do with a try-catch block but am at a loss when I want to implement this the aforementioned method throwing the exception. Please help and thank you!
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Assignment2
{
/**
* Converts a binary string into a decimal int and
* back into a String
*
* @param binaryString a string to hold binary string
* input from user
* @return decimalString String to hold converted decimal
* value from binaryString
*/
private static String parseBinary(String binaryString)
{
int decimalValue = Integer.parseInt(binaryString, 2);
String decimalString = Integer.toString(decimalValue);
return decimalString;
}
/**
* GUI interface for user input for method
* parseBinary
*
*/
public static void main(String[] args)
{
JFrame frame = new JFrame("Convert Binary to Decimal");
JPanel panel = new JPanel();
panel.setLayout(null);
JButton b1 = new JButton("Convert to Decimal");
b1.setBounds(0, 125, 593, 30);
JTextField txt1 = new JTextField(30);
txt1.setBounds(120, 20, 470, 30);
JTextField txt2 = new JTextField(30);
txt2.setBounds(120, 60, 470, 30);
JLabel lbl1 = new JLabel("Binary String");
lbl1.setBounds(10, 20, 300, 30);
JLabel lbl2 = new JLabel("Decimal String");
lbl2.setBounds(10, 60, 300, 30);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(600, 200);
panel.add(b1);
panel.add(txt1);
panel.add(txt2);
panel.add(lbl1);
panel.add(lbl2);
frame.add(panel);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
// converts user input from the first text field one and
// displays it in the second text field
txt2.setText("" + parseBinary(txt1.getText()));
}
catch(NumberFormatException ex)
{
// finds invalid digit entered
int temp = Integer.parseInt(txt1.getText());
int firstInvalidDigit = 0;
while(temp>0)
{
int rem = temp%10;
if(rem>1)
{
firstInvalidDigit = rem;
}
temp = temp/10;
}
txt2.setText("Invalid format for a binary string. Illegal character: " + firstInvalidDigit);
}
}
});
}
}
Explanation / Answer
Hi Sir, I have added my comments inline in the program code for your better understandings. Look for comments that say "GOPAL ADDED COMMENTS HERE" where I have put comments for explaining the purpose.
Here is your original code along with my comments added:-
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.JPanel;
import javax.swing.JTextField;
public class Assignment2 {
/**
*
* Converts a binary string into a decimal int and
*
* back into a String
*
*
*
* @param binaryString
* a string to hold binary string
*
* input from user
*
* @return decimalString String to hold converted decimal
*
* value from binaryString
*
*/
private static String parseBinary(String binaryString) {
int decimalValue = Integer.parseInt(binaryString, 2);
String decimalString = Integer.toString(decimalValue);
return decimalString;
}
/**
*
* GUI interface for user input for method
*
* parseBinary
*
*
*
*/
public static void main(String[] args){
JFrame frame = new JFrame("Convert Binary to Decimal");
JPanel panel = new JPanel();
panel.setLayout(null);
JButton b1 = new JButton("Convert to Decimal");
b1.setBounds(0, 125, 593, 30);
JTextField txt1 = new JTextField(30);
txt1.setBounds(120, 20, 470, 30);
JTextField txt2 = new JTextField(30);
txt2.setBounds(120, 60, 470, 30);
JLabel lbl1 = new JLabel("Binary String");
lbl1.setBounds(10, 20, 300, 30);
JLabel lbl2 = new JLabel("Decimal String");
lbl2.setBounds(10, 60, 300, 30);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(600, 200);
panel.add(b1);
panel.add(txt1);
panel.add(txt2);
panel.add(lbl1);
panel.add(lbl2);
frame.add(panel);
/* GOPAL COMMENTS HERE:-
* You should trigger the error message handling part when the button is clicked. In the button Handler
* (Convert to Decimal Button) code, add a try catch block which will parse the user input and check if any
* invalid or special characters have been input, apart from 0/1
*/
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
// converts user input from the first text field one and
// displays it in the second text field
/* GOPAL ADDED COMMENTS HERE:-
* call parseBinary() and pass the user input, if any exception arises, we will goto Catch Handler.
* In catch handler we will display the error message in textbox2.
*/
txt2.setText("" + parseBinary(txt1.getText()));
}catch (NumberFormatException ex){
// finds invalid digit entered
int temp = Integer.parseInt(txt1.getText());
int firstInvalidDigit = 0;
while (temp > 0){
int rem = temp % 10;
if (rem > 1){
firstInvalidDigit = rem;
}
temp = temp / 10;
}
/* GOPAL ADDED COMMENTS HERE:-
* Here we display the error message in textbox 2 about the illegal character entered.
*/
txt2.setText("Invalid format for a binary string. Illegal character: " + firstInvalidDigit);
}
}
});
}
}
Please let me know in case of any clarifications required. Thanks!