Results 1 to 16 of 16
  1. #1
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Check String for int's

    I'm trying to check a JTextField for int's, but it doesn't work.

    I get these answers:

    Hello = false;
    123 = true;
    Hello123 = true;


    Java Code:
    public boolean noInt(JTextField tf) { 
        boolean working = false; 
        String text = tf.getText().toString(); 
        for(int i = 0; i < text.length(); i++) { 
            if(Character.isDigit(text.charAt(i))) { 
                working = true; 
            } 
        } 
        if(working == false) { 
            JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!"); 
        } 
         
        else { 
            JOptionPane.showMessageDialog(null, "It's Working!"); 
        } 
        return working; 
    }

  2. #2
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Check String for int's

    PHP Code:
    try {
         Long.parseLong(text.getText());
    JOptionPane.showMessageDialog(null, "It's Working!");
    } catch(Exception ex) {
         JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!"); 
    }

  3. #3
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Check String for int's

    With your code:

    PHP Code:
    public boolean noInt(JTextField tf) { 
        boolean working = true; 
        String text = tf.getText(); 
    
        for(int i = 0; i < text.length(); i++) { 
            if(!Character.isDigit(text.charAt(i))) { 
                working = false;
                break;
            } 
        } 
    
        if(working == false) { 
            JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!"); 
        } else { 
            JOptionPane.showMessageDialog(null, "It's Working!"); 
        }
     
        return working; 
    }
    Last edited by DIego912; 01-06-2012 at 01:32 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Check String for int's

    After your code has run through the loop your boolean variable is true if at least one of the characters in the String was a digit (e.g. foo123bar). Do you want that?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Check String for int's

    Quote Originally Posted by DIego912 View Post
    PHP Code:
    try {
         Long.parseLong(text.getText());
    JOptionPane.showMessageDialog(null, "It's Working!");
    } catch(Exception ex) {
         JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!"); 
    }
    I still get the same answer...

  6. #6
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Check String for int's

    Sorry, I misunderstood the request...

    PHP Code:
    public boolean noInt(JTextField tf) { 
        boolean working = false; 
        String text = tf.getText(); 
     
        for(int i = 0; i < text.length(); i++) { 
            if(Character.isDigit(text.charAt(i))) { 
                working = true;
                break;
            } 
        } 
     
        if(working == false) { 
            JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!"); 
        } else { 
            JOptionPane.showMessageDialog(null, "It's Working!"); 
        }
      
        return working; 
    }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Check String for int's

    That is functionally equivalent to the original code and the OP doesn't want that ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Check String for int's

    Quote Originally Posted by JosAH View Post
    After your code has run through the loop your boolean variable is true if at least one of the characters in the String was a digit (e.g. foo123bar). Do you want that?

    kind regards,

    Jos
    But it gave me true when I typed in Hello123Hello aswell,

    This is how I want it:

    Hello = true;
    123Hello = false;
    123 = false;
    Hello123 = false;

    Do you have any tip :)?
    Last edited by JultomteN; 01-06-2012 at 02:20 PM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Check String for int's

    Quote Originally Posted by JultomteN View Post
    But i gave me true when I typed in Hello123Hello aswell
    That's what I wrote: your original code is happy if at least one digit is present in the code. It seems that you want all characters to be digits.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Check String for int's

    Surely just parse the text to an integer or whatever and handle the exception?

  11. #11
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Check String for int's

    At codeline 5 I would like to have if(Character.isDigit(text.charAt(i) == true)), then it would check if every char is a digit. But that doesn't compile

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Check String for int's

    That's because you have your brackets in the wrong place:
    if(Character.isDigit(text.charAt(i)) == true)

  13. #13
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Check String for int's

    When I do it like this, i get;

    Hello = true;
    Hello123 = true;
    123 = false;

    So it's on the right way, I only want it to be letters, but I can't get it right =/

    Java Code:
    public boolean noInt(JTextField tf) {
        boolean working = false;
        String text = tf.getText().toString();
        for(int i = 0; i < text.length(); i++) {
            if(Character.isDigit(text.charAt(i)) == false) {
                working = true;
            }
        }
        if(working == false) {
            JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!");
        }
        
        else {
            JOptionPane.showMessageDialog(null, "It's Working!");
        }
        return working;
    }

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Check String for int's

    You're backwards I'd say.
    You want to know if there is a digit in the string, so assume the string has no digits first:
    Java Code:
        boolean noDigit = true;
    then loop over the characters as you are, but set the noDigit flag to false if you encounter a digit. Also change the loop criteria to take this into account:
    Java Code:
    for (int i = 0; i < text.length() && noDigit; i++) {
    etc etc
    }

  15. #15
    JultomteN is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Check String for int's

    Quote Originally Posted by Tolls View Post
    You're backwards I'd say.
    You want to know if there is a digit in the string, so assume the string has no digits first:
    Java Code:
        boolean noDigit = true;
    then loop over the characters as you are, but set the noDigit flag to false if you encounter a digit. Also change the loop criteria to take this into account:
    Java Code:
    for (int i = 0; i < text.length() && noDigit; i++) {
    etc etc
    }
    Like this?

    Java Code:
    public boolean noInt(JTextField tf) {
        boolean noDigit = true;
        String text = tf.getText().toString();
        for(int i = 0; i < text.length() && noDigit; i++) {
            if(Character.isDigit(text.charAt(i))) {
                noDigit = false;
            }
        }
        if(noDigit == false) {
            JOptionPane.showMessageDialog(null, "Please use only letters in the name textfield!");
        }
        
        else {
            JOptionPane.showMessageDialog(null, "It's Working!");
        }
        return noDigit;
    }

  16. #16
    Ziplock is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Re: Check String for int's

    You could just check the whole input at once and try to parse it as an int, and if it fails then you know it contains letters or invalid characters.

    Java Code:
        public static void main(String[] args) {
            String text = in.nextLine();
            try
            {
                Integer.parseInt(text);
                System.out.println("Text is numerical.");
            }
            catch (Exception e)
            {
                System.out.println("Text is not numerical.");
            }
        }

Similar Threads

  1. Check if string is numbe
    By carman12 in forum New To Java
    Replies: 3
    Last Post: 02-02-2011, 02:16 AM
  2. check string inside string
    By alacn in forum New To Java
    Replies: 8
    Last Post: 07-07-2010, 01:52 PM
  3. Replies: 1
    Last Post: 04-30-2010, 04:21 PM
  4. How to check String confirmation with integer
    By Basit56 in forum New To Java
    Replies: 4
    Last Post: 01-06-2010, 12:35 PM
  5. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •