1

This is my first post on StackOverflow. I'm having an issue within my program for setting up a decimal-hex-binary converter. The idea is to have three textfields: decimal, hex, binary on top of each other(in one VBox) and the labels: decimal, hex, binary, on matching those text field positions. I've acquired that with this code:

GridPane paneForVBoxes = new GridPane();
        paneForVBoxes.setPadding(new Insets(10, 10, 10, 10));
        paneForVBoxes.setVgap(10);
        paneForVBoxes.setHgap(10);

        VBox units = new VBox(12);
        units.setAlignment(Pos.CENTER_LEFT);
        Label lbDec = new Label("Decimal");
        Label lbHex = new Label("Hex");
        Label lbBin = new Label("Binary");
        units.getChildren().addAll(lbDec, lbHex, lbBin);


        VBox textFields = new VBox(5);
        TextField tfDec = new TextField();
            tfDec.setPrefColumnCount(19);
            tfDec.setAlignment(Pos.CENTER_RIGHT);
        TextField tfHex = new TextField();
            tfHex.setPrefColumnCount(19);
            tfHex.setAlignment(Pos.CENTER_RIGHT);
        TextField tfBin = new TextField();
            tfBin.setPrefColumnCount(19);
            tfBin.setAlignment(Pos.CENTER_RIGHT);

        textFields.getChildren().addAll(tfDec, tfHex, tfBin);

        paneForVBoxes.add(units, 0, 0);
        paneForVBoxes.add(textFields, 1, 0);

So the idea is to then give a live update of conversion to each textfield as you type in a value, for example: In the dec textfield you type 11, in the hex and bin fields 1A and 1011 automatically appear, respectively.

I achieve this to a point, but when I delete all characters in my textfields I receive NumberFormatExceptions and I have no idea how to get around this. This is what I've attempted to fix the problem: please note This is only the coding for decimal to binary fields, with a simple fix to this I can apply that to the rest of my code that has yet to be developed. note also I have tried Integer.parseInt(s) within and around tfDec.setText("0"); but eclipse truly does not like that

 tfDec.setOnKeyReleased(e -> {
                if (tfDec.equals("")) {
                    tfDec.setText("0");
                }
                else {
                    String bin = Integer.toBinaryString(Integer.parseInt(tfDec.getText()));
                    tfBin.setText(bin);
                }
            });

/Whole code/

    package application;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Elias16_5 extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane paneForVBoxes = new GridPane();
            paneForVBoxes.setPadding(new Insets(10, 10, 10, 10));
            paneForVBoxes.setVgap(10);
            paneForVBoxes.setHgap(10);

            VBox units = new VBox(12);
            units.setAlignment(Pos.CENTER_LEFT);
            Label lbDec = new Label("Decimal");
            Label lbHex = new Label("Hex");
            Label lbBin = new Label("Binary");
            units.getChildren().addAll(lbDec, lbHex, lbBin);


            VBox textFields = new VBox(5);
            TextField tfDec = new TextField();
                tfDec.setPrefColumnCount(19);
                tfDec.setAlignment(Pos.CENTER_RIGHT);
            TextField tfHex = new TextField();
                tfHex.setPrefColumnCount(19);
                tfHex.setAlignment(Pos.CENTER_RIGHT);
            TextField tfBin = new TextField();
                tfBin.setPrefColumnCount(19);
                tfBin.setAlignment(Pos.CENTER_RIGHT);

            textFields.getChildren().addAll(tfDec, tfHex, tfBin);

            paneForVBoxes.add(units, 0, 0);
            paneForVBoxes.add(textFields, 1, 0);

            tfDec.setOnKeyReleased(e -> {
                if (tfDec.equals("")) {
                    tfDec.setText("0");
                }
                else {
                    String bin = Integer.toBinaryString(Integer.parseInt(tfDec.getText()));
                    tfBin.setText(bin);
                }
            });







            Scene scene = new Scene(paneForVBoxes, 300, 100);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setTitle("Data Conversion");
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
David
  • 81
  • 1
  • 1
  • 7

3 Answers3

0

You should try StringUtils. So this code:

if (tfDec.equals("")) {

will be

if (StringUtils.isBlank(tfDec.getText())) {

Read more here: StringUtils.isBlank() vs String.isEmpty()

Also, in the first line, it should be

tfDec.getText().equals("")

not

tfDec.equals("")
Community
  • 1
  • 1
m.aibin
  • 3,528
  • 4
  • 28
  • 47
  • also, in the first line, it should be tfDec.getText().equals(""), not tfDec.equals("") – m.aibin Oct 23 '15 at 05:00
  • Eclipse doesn't like StringUtils, it says it cannot be resolved, I've tried looking for imports but none are available. Any suggestions? – David Oct 23 '15 at 05:08
  • try to convert your project into Maven project, than you will have the possibility to add different jars as a dependencies. http://stackoverflow.com/questions/2449461/convert-existing-eclipse-project-to-maven-project – m.aibin Oct 23 '15 at 05:10
  • I'm afraid that's outside of the scope of this program, I would love to do that in a different circumstance, as I'm still learning Java. I just don't believe it would do me well for this assignment, I'd up vote if I could though! – David Oct 23 '15 at 05:12
  • You could also get this class directly from here :D https://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/StringUtils.html And add as a Java class in your project ;) – m.aibin Oct 23 '15 at 05:13
  • If you have Maven project, the process is very simple. You need to open pom.xml file and add this dependency: http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.0 How? Read this: http://stackoverflow.com/questions/396245/add-a-dependency-in-maven or : https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html – m.aibin Oct 23 '15 at 05:16
  • your tfDec.getText().equals("") works just fine! Thank you it works exactly as I would like. – David Oct 23 '15 at 05:19
0

I am not sure if you have studied "try/catch" blocks. If so, then rather than trying to prevent the exception, why not catch the exception and deal with it. For example if someone types "G" and the exception occurs, then you can print "invalid" in the bin field and/or pop up a dialog that warns the users that only 0-9 are allowed in the decimal text field.

You have the statement tfDec.equals(""). Does that ever come back true? I am really surprised that a TextField can be compared to a string in the first place. I would have expected more like tfDec.getText().equals("");

Teto
  • 475
  • 2
  • 10
  • Any args inside a tf is natively a string, that being said any tf can be compared to a string. I suppose it's not very efficient though. – David Oct 23 '15 at 05:09
  • Being symmetric is a part of the contract for overriding equals(). Symmetry requires that for any non-null references x and y, if x.equals(y) then y.equals(x). That won't be true in this case because even if tfDec.equals(someString) returns true, someString.equals(tfDec) will not be true and might throw ClassCastException. – Teto Oct 23 '15 at 17:51
0

Though setOnKeyReleased() works in this scenario but this is not the optimum solution. This method will be triggered whenever any key is released whether it makes any difference to the text in the textfield or not. For example, when the textfield is empty and the user is pressing BackSpace.

A better approach will be to add a change listener on the textProperty() of the TextField. This will only be triggered when the text of the textfield changes :

tfDec.textProperty().addListener((observable, oldValue, newValue) -> {
     if (!newValue.isEmpty()) {
         tfBin.setText(Integer.toBinaryString(Integer.parseInt(newValue)));
     } else {
         tfBin.setText("0");
     }
});
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176