Check if imput is Int in a netbeans interface

Asked

Viewed 38 times

-1

Good evening! I am making a program with Netbeans IDE interface. I need to compare if the input data in the text field where it should be said which quantity is a String because I just wanted to make available input Int values! I would like to know if there is a comparison command for this. I have already searched but I have not found any results in fact. If you can help me, I appreciate it! Follow the button code that adds in the database the entered values.

private void cad_cadActionPerformed(java.awt.event.ActionEvent evt) {                                        

    Produtos produto = new Produtos();
    produto.setFabricante(cad_fabricante.getText());
    produto.setModelo(cad_modelo.getText());
    produto.setValor(Integer.parseInt(cad_valor.getText()));
    produto.setEstoque(Integer.parseInt(cad_qtd.getText()));

    if(cad_fabricante.getText().isEmpty() || cad_modelo.getText().isEmpty() || cad_valor.getText().isEmpty()||cad_qtd.getText().isEmpty()){
    JOptionPane.showMessageDialog(null, " Todos os campos devem ser preenchidos.");
    }else if(--------->A COMPARAÇÃO ENTRARIA AQUI <-----------)){
     JOptionPane.showMessageDialog(null,"Os Campos VALOR e ESTOQUE devem ser preenchidos com números.");

    }else{
    ProdutosDAO dao = new ProdutosDAO();
    dao.AdicionarPeca(produto);
    JOptionPane.showMessageDialog(null,"Produto " + cad_fabricante.getText()+ " adicionado com sucesso!" );

    }
    cad_fabricante.setText("");
    cad_modelo.setText("");
    cad_valor.setText("");
    cad_qtd.setText("");

}   

1 answer

1

Thinking about input conditions in java, how can we verify if a given value is Integer?

Integer.parseInt(valor)

of course java is a strongly typed language and this helps on inputs, but when it comes to users we know that we can request an input for example: " type a number" and the user type "ONE" so on...

in relation to your question we can make this conversion, but it will release an exception, in which case we can use regular expressions.

I created the Product class with their respective attributes.


public class Product {

    private String fabricante;
    private String modelo;
    private Integer valor;
    private Integer qtd;

    public Product(String fabricante, String modelo, Integer valor, Integer qtd) {
        this.fabricante = fabricante;
        this.modelo = modelo;
        this.valor = valor;
        this.qtd = qtd;
    }

    public Product() {}

    //get and setters

}


Soon after I created a class to test this class, where I used a condition with regular expression.

import javax.swing.JOptionPane;

public class TestProduct {
    public static void main(String[] args) {
        String valor = null;
        Integer val = null;
        boolean condicao = true;
        Product product = new Product();

         product.setFabricante(JOptionPane.showInputDialog(null, "Qual fabricante"));
         product.setModelo(JOptionPane.showInputDialog(null, "Qual modelo?"));

         try {
             valor = JOptionPane.showInputDialog(null, "Digite o valor em numeral");
             while(condicao) {
                 if(valor.matches("[0-9]*")) {
                     val = Integer.parseInt(valor);
                     condicao = false;
                 }
                 else {
                     valor = JOptionPane.showInputDialog(null, "Digite o valor em numeral");
                 }
             }

         } catch (Exception e) {
            System.out.println(e);
         }


        product.setValor(val);

        System.out.println(product.getFabricante());
        System.out.println(product.getValor());
    }

}

then in the end just make your insertions using JPA.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.