wood burning stoves*
The moose likes Beginning Java and the fly likes Checking if a string has numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking if a string has numbers" Watch "Checking if a string has numbers" New topic
Author

Checking if a string has numbers

Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
Hello,
I was working on an exercise to check if a given string contains any numbers. I am not allowed to use regex. I wanted to know if there is a better solution of what I have. I feel mine has an extra for loop. Please suggest.



dR
Ashwin Soni
Greenhorn

Joined: Jun 22, 2012
Posts: 18

Well for better approach you can try using if-else condition
e.g.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Take a look at the String.charAt and Character.isDigit methods. They should help yopu simplify it.
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Try this



Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

Eswar Nec wrote:Try this

Eswar, please don't hand out ready-made solutions. This site is NotACodeMill and we advise people to DoYourOwnHomework.

I suggest you read the guidelines on the HowToAnswerQuestionsOnJavaRanch page.

Thanks.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
Eswar Nec wrote:Try this



Can you please let me the logic or point me where I can improve my code.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Raghu Devatha wrote:Can you please let me the logic or point me where I can improve my code.

Stuart A. Burkett wrote:Take a look at the String.charAt and Character.isDigit methods. They should help you simplify it.
Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
Thanks Stuart A. Burkett.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Ashwin Soni wrote: . . .
Several points where you can improve that code style.
  • 1: Don’t use number literals. If you are checking for chars, use those chars. '0' and '9'
  • 2: If you use '0' and '9' you want >= and <= rather than < and >
  • 3: Beware of saying if (...) b = true; if you can say isNum = isNum && (ch >= '0' //etc

  • Now work out why I wrote etc rather than the full expression and identify the logical error in what you posted!

    Checking whether all the characters is a String are digits can only confirm the String is an integer. It is much more complicated for floating‑point numbers. What about Strings like "  123" or "‑123"? Do they represent numbers?
    Ashwin Soni
    Greenhorn

    Joined: Jun 22, 2012
    Posts: 18

    Campbell Ritchie wrote:
    Ashwin Soni wrote: . . .
    Several points where you can improve that code style.
  • 1: Don’t use number literals. If you are checking for chars, use those chars. '0' and '9'
  • 2: If you use '0' and '9' you want >= and <= rather than < and >
  • 3: Beware of saying if (...) b = true; if you can say isNum = isNum && (ch >= '0' //etc

  • Now work out why I wrote etc rather than the full expression and identify the logical error in what you posted!

    Checking whether all the characters is a String are digits can only confirm the String is an integer. It is much more complicated for floating‑point numbers. What about Strings like "  123" or "‑123"? Do they represent numbers?


    Thanks for Suggestion

    Well my approach was this

    Stuart A. Burkett
    Ranch Hand

    Joined: May 30, 2012
    Posts: 679
    There's a bug in that code. Are you sure it handles all numbers ?

    Once you've found it, I would recommend using '0' and '9' rather than the ASCII values. It makes it more clear what you are testing for.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    One error down , one to go. I have already given you a hint about that error.

    I think your loop will simply mark all the integer characters in your text. If you write something like ABCD1234, it will find the 1234. Probably as 1 2 3 and 4 separately.
    Ashwin Soni
    Greenhorn

    Joined: Jun 22, 2012
    Posts: 18

    Stuart A. Burkett wrote:There's a bug in that code. Are you sure it handles all numbers ?

    Once you've found it, I would recommend using '0' and '9' rather than the ASCII values. It makes it more clear what you are testing for.


    Yes it couldn't find '0' in a string.



    And I want to know why shouldn't use ascii for this approach?

    Ashwin Soni
    Greenhorn

    Joined: Jun 22, 2012
    Posts: 18

    Campbell Ritchie wrote:One error down , one to go. I have already given you a hint about that error.

    I think your loop will simply mark all the integer characters in your text. If you write something like ABCD1234, it will find the 1234. Probably as 1 2 3 and 4 separately.


    Well output of code is:
    String has a number and that number is: 1
    String has a number and that number is: 2
    String has a number and that number is: 3
    String has a number and that number is: 8
    String has a number and that number is: 9
    String has a number and that number is: 4
    String has a number and that number is: 5
    String has a number and that number is: 6
    String has a number and that number is: 2
    String has a number and that number is: 3
    String has a number and that number is: 5
    String has a number and that number is: 2
    String has a number and that number is: 2
    String has a number and that number is: 0

    it is not printing like 12389456235220

    OR
    Stuart A. Burkett
    Ranch Hand

    Joined: May 30, 2012
    Posts: 679
    Ashwin Soni wrote:And I want to know why shouldn't use ascii for this approach?

    I'm not saying you shouldn't. I'm just saying that the following would make it more obvious what the purpose of the code was.

    Even better would be
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    that is what I thought would happen. I think you will have to go back to your earlier work when you had the isNumber boolean. Have a look at my hints.

    The reason for not using 48 should by now be obvious; it is too error‑prone. Using < and <= needs care, because it is possible to have an off‑by‑one error, which is exactly what happened to you.
    If . . .
  • We allow an integer number to start with -
  • We find a String method which allows us to remove whitespace from its beginning and end
  • We can work out whether a String is in the correct format for an integer (positive and negative). Write down on a piece of paper how you intend to work out whether these Strings are integers or not:
    123
    0
    000
    -123
    1234567890a
    number1
    1.2
    It is much easier if you confine yourself to decimal numbers.
    By the way, I think 000 is not a valid integer.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    As an alternative, consider (as Java does) that all integer literals are non‑negative, and reagrd a leading - as a sign‑change operator.
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11175
        
      16

    Campbell Ritchie wrote:By the way, I think 000 is not a valid integer.

    would 00100 be a valid integer?

    IIRC, one of the problems with using numeric literals is that if your code runs on a machine that uses something other than ascii, your values may not be correct. Also, I don't think you are guaranteed that the digits are always consecutive in all encodings.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    fred rosenberger wrote: . . . would 00100 be a valid integer? . . .
    Not in my scheme of things.

    It might be in somebody else’s.
    Pankaj Thaku
    Greenhorn

    Joined: May 02, 2012
    Posts: 1

    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7554
        
      18

    Pankaj Thaku wrote:public class Test{...

    Erm, and this is supposed to be what? A solution? If so, I suggest you check the HowToAnswerQuestionsOnJavaRanch page.

    However, despite that, welcome Pankaj. I'm sure your motives were laudable, but I'm afraid we're NotACodeMill (←click).

    Also, before you post code again, please read the UseCodeTags page.

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    Welcome again
    I have added code tags.
    There are several serious stylistic errors
  • Use of the non-specific name for a local variable flag
  • The flag variable is false if a number is found; that is confusing.
  • Use of == false; you should use if (!flag)... == false and == true are very error‑prone.
  • Too much code in a main method, but maybe for a little application like that...
  • Unnecessary casts
  • Not enough whitespace between successive tokens
  • I think the use of break; is a serious design error, but I know other people will disagree with me. And I shall pretend not to notice you have repeated the serious logical error which will miss some numbers, which happened earlier in this thread.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Checking if a string has numbers
     
    Similar Threads
    program correction
    prime numbers -please help
    why is the error coming only for j not for i?
    Simple program: NEED HELP!!
    which break statement doulh I use