Im trying to input 11 digit numbers but i got an error when parsing from string to int
 JLabel lblMobile = new JLabel("Mobile No. :");  
add(lblMobile);

 JTextField txtMobile = new JTextField("");
add(txtMobile);




  int CUSMobnum = Integer.parseInt(txtMobile.getText());

Dani AI

Generated

As discovered, trying to parse an 11‑digit phone number into an int will fail. and were right to question the approach: phone numbers are identifiers, not quantities you do arithmetic on. Practical, low‑effort fixes and a small UI improvement follow.

Treat the value as a String and validate it instead of parsing to a numeric type. For a simple 11‑digit check:

String mobile = txtMobile.getText().trim();
if (!mobile.matches("\\d{11}")) {
    // show error: must be exactly 11 digits
} else {
    // store/use mobile as a String
}

To prevent bad input at the UI level, apply a DocumentFilter that allows only digits and limits length to 11. Example filter and attachment:

import javax.swing.text.*;

class DigitLimitFilter extends DocumentFilter {
    private final int max;
    DigitLimitFilter(int max) { this.max = max; }
    private String digitsOnly(String s) { return s == null ? "" : s.replaceAll("\\D+", ""); }

    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
        string = digitsOnly(string);
        if (fb.getDocument().getLength() + string.length() <= max) super.insertString(fb, offset, string, attr);
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        text = digitsOnly(text);
        int newLen = fb.getDocument().getLength() - length + text.length();
        if (newLen <= max) super.replace(fb, offset, length, text, attrs);
        else {
            int allowed = max - (fb.getDocument().getLength() - length);
            if (allowed > 0) super.replace(fb, offset, length, text.substring(0, allowed), attrs);
        }
    }
}

// attach with:
((AbstractDocument) txtMobile.getDocument()).setDocumentFilter(new DigitLimitFilter(11));

Notes: consider international formats (leading '+', country codes) and store a normalized representation (for example E.164) in your database. For robust parsing/formatting across locales, use a library designed for phone numbers (e.g., Google's libphonenumber).

Recommended Answers

All 3 Replies

it only accept 9 digits.. and i want to insert 11 digit numbers..

you get an error on 11 digits, because an int can not store that big a value. please choose another data type.
in your above code, there is no restriction whatsoever on the field, and saying "I got an error" is of very little use if you don't say what kind of error.
in next posts, please provide some more information. it'll be easier for us to help you out.

There's no reason to use an integer field to store phone numbers. You won'r be doing arithmetic with them.
Most people prefer to enter and see them with some formatting, eg a - or blanks between groups of digits. There is also the convention for international prefixes that looks like "+33 (0)5 53 etc". Those are all very good reasons to hold the original String, and not try to parse it as integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.