#only accept int or string

13 messages · Page 1 of 1 (latest)

south lagoon
#

I need heellpp

blazing torrentBOT
#

This post has been reserved for your question.

Hey @south lagoon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

slim snow
#

jeez atleast type in what you need help with

dapper glade
#

@south lagoon What do you need help with?

south lagoon
#

hi. I am looking for an answer on how to only accept integer in the text field for qty and stockno

dapper glade
#

Gosh that hurts my eyes. I can't even read it lmao

south lagoon
#

what you mean?

south lagoon
#

private void btnadddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String stockk = stockno.getText();
String unitt = unit.getText();
String descc = desc.getText();
String qtyy = qty.getText();

    //String entityy = entity_name.getText();
    //String divisionn = division.getText();
    //String officee = office.getText();
    //String fundd = fund.getText();
    //String center_codee = center_code.getText();
    //String risnum = risno.getText();

    int stockkk = Integer.parseInt(stockk);
    int qtyyy = Integer.parseInt(qtyy);


    try {
        pst = con.prepareStatement("INSERT INTO ris (id,unit,descript,qty)VALUES(?,?,?,?)");
        //pst = con.prepareStatement("INSERT INTO entity (entity,division,office,fund,respo_center_code,risno)VALUES(?,?,?,?,?,?)");
        pst.setInt(1,stockkk);
        pst.setString(2,unitt);
        pst.setString(3,descc);
        pst.setInt(4,qtyyy);
        //pst.setString(1,entityy);
        //pst.setString(2,divisionn);
        //pst.setString(3,officee);
        //pst.setInt(4,funddd);
        //pst.setInt(5,center_codeee);
        //pst.setInt(6,risnumm);

        int k = pst.executeUpdate();

        if(k==1){
            JOptionPane.showMessageDialog(this,"success");
            
            stockno.setText("");
            unit.setText("");
            desc.setText("");
            qty.setText("");
            Fetch();
                    LoadProductNo();
            
        }else {
            JOptionPane.showMessageDialog(this,"error");
        }
                } catch (SQLException ex) {
        Logger.getLogger(HARDWARE.class.getName()).log(Level.SEVERE, null, ex);
    }
}
#

is there a way for me to restrict the user to only input INT in the stockk and qtyyy text field?

slim snow
#

you can use JFormattedTextField java NumberFormat format = NumberFormat.getInstance(); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); formatter.setMinimum(0); formatter.setMaximum(Integer.MAX_VALUE); formatter.setAllowsInvalid(false); // If you want the value to be committed on each keystroke instead of focus lost formatter.setCommitsOnValidEdit(true); JFormattedTextField field = new JFormattedTextField(formatter);

#

@south lagoon