-1

" NumberFormatException error occurs when i click the save button but i am not converting a string into a number. "

i tried :

try {
result = Integer.parseInt(input);
} catch(Exception e) {
if(input == null || input.isEmpty()) { // case 1a
return 0; //case 1a
} 

try {
String sql = "INSERT INTO c_records (job_id,u_name,model,warranty,deposit,balance,date,status,technician,balance_paid,clearance_date) VALUES (?,?,?,?,?,?,?,?,?,?,?)";   


pst = connect.prepareStatement(sql);



pst.setInt(1, Integer.valueOf(jTextField1.getText()));
pst.setString(2, jTextField2.getText());
pst.setString(3, jTextField3.getText());
pst.setInt(4, Integer.valueOf(jTextField4.getText()));
pst.setInt(5, Integer.valueOf(jTextField5.getText()));
pst.setInt(6, Integer.valueOf(jTextField6.getText()));
pst.setString(7, jTextField7.getText());
pst.setString(8, jTextField8.getText());
pst.setString(9, jTextField9.getText());
pst.setInt(10, Integer.valueOf(jTextField10.getText()));
pst.setString(10, jTextField10.getText());
pst.setString(11, jTextField11.getText());



pst.execute();

JOptionPane.showMessageDialog(null, "Record Saved!");
        }
        catch(Exception e)
           {
           JOptionPane.showMessageDialog(null, e);
           }

i want the send button the send the data to the DB.

Safak Ozdek
  • 906
  • 11
  • 18
pol
  • 25
  • 3
  • "i am not converting a string into a number" - you're calling `Integer.parseInt` and `Integer.valueOf`, both of which are converting strings into numbers... – Jon Skeet Jul 16 '19 at 12:18
  • if i set them as a string, it brings errors. – pol Jul 16 '19 at 12:24
  • how do i go about sending the integer values to the DB. – pol Jul 16 '19 at 12:26
  • Well presumably one of your text fields is empty - what do you want to do at that point? – Jon Skeet Jul 16 '19 at 12:56
  • @JonSkeet, the question says that if the text field is empty, the questioner wants to set the value to 0 -- see case 1a in the first block of code. It is unclear when that block is getting called, because if it were guarding against empty, it should work. pol, when you say you tried that first block, at what stage did you try it? – Jeremy Kahan Jul 16 '19 at 13:08
  • @JeremyKahan: Ah, I missed that. Presumably they should be calling that method instead of just calling `Integer.valueOf` then... – Jon Skeet Jul 16 '19 at 13:19
  • Before converting the string to Integer `create a method` which `validates the input` Ex: `jTextField2.getText()`, Test/Verify the input is not null or empty and it is the number. – Dushyant Tankariya Jul 16 '19 at 13:24

3 Answers3

1

i added this to the catch parameter

 catch(HeadlessException | NumberFormatException | SQLException e)

also the date format was wrong when i was entering data so i changed it.

lastly , i removed:

pst.setString(10, jTextField10.getText());

pol
  • 25
  • 3
0

Note Integer is in the range of -2,147,483,648 to +2,147,483,647

If you pass anything that is above integer or a string that has characters you get that error;

Case 1

Integer.parseInt("1232322132434")

Error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1232322132434"

Case 2:

Integer.parseInt("1232w")


Exception in thread "main" java.lang.NumberFormatException: For input string: "1232w"
  • Also avoid special characters. Don't enter a balance like 32,567 – Chris Kibuchi Jul 16 '19 at 13:12
  • note that in the title the user had the problem of passing an empty string, presumably to parseInt. But what is not clear is that the user also appears to have tried checking for an empty string, which should have helped. – Jeremy Kahan Jul 16 '19 at 13:48
  • I advice that Pol should use e.printStackTrace() function to echo the exact source of his problem. The actual line with the error – Chris Kibuchi Jul 17 '19 at 06:47
0

So what you attempted should work, but it looks like you must not have been calling it when you think (perhaps because you were not thinking that valueOf calls parseInt). You could write it as a little function like:

public static Integer safeValueOf(String s) {
    Integer result = null; // signals error of unanticipated kind
    try {
        result = Integer.parseInt(s);
    } catch (NumberFormatException e) {
        if (s == null || s.isEmpty()) { // case 1a
            result = 0; //case 1a
        }
    } finally {
        return result;
    }

and then replace lines like pst.setInt(4, Integer.valueOf(jTextField4.getText())); with

pst.setInt(4, safeValueOf(jTextField4.getText()));
Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23