eCharcha.Com   Support eCharcha.Com. Click on sponsor ad to shop online!

Advertise Here

Go Back   eCharcha.Com > Science and Technology > How to

Notices

How to Stuck for an answer to "How to do .."?

Reply
 
Thread Tools Display Modes
  #1  
Old December 2nd, 2003, 10:08 AM
echarcha's Avatar
echarcha echarcha is offline
Sutradhar {admin}
 
Join Date: Jul 2000
Location: CA, USA
Posts: 45,295
echarcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond repute
Question Java query : How to find if a String contains only numbers?

Folks,
If this was PERL, PHP or C++ then it was very starightforward.
as Regular Expressions would have come to the rescue.

But I am not aware how this is done in Java and hence the question.

I have two strings, say, "Hello123" and "123"

Now I wish to find out if the strings represents a number and can be converted to a number.

For example, in case of "Hello123", it cannot be a representation of a number because it contains alphabets along with numerals.

But the string "123" is a string representing a number.

Please tell me how do I go about finding if a String has only numbers in it?

I tried the
Code:
Integer.parseInt()
method but this method will throw an exception if the String is not a number. And catching an exception and deciding that its not a number is not a good way to go about it.

Then there is the ugly method where you use the
Code:
String.indexOf()
method and test for numbers from 0 to 9. This is also not a efficient way of doing it. If the String is long with many numbers, then this technique will really slow down the process.

Please help folks.

Thanks,
Sunit
__________________
eCharcha.com
-Loud and Proud Desi Opinions
Reply With Quote
  #2  
Old December 2nd, 2003, 11:28 AM
Charchila's Avatar
Charchila Charchila is offline
Temporarily Unbanned
 
Join Date: Sep 2001
Location: Ahmedabad, CA
Posts: 7,760
Charchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond reputeCharchila has a reputation beyond repute
something like this should work

Code:
public boolean isNumber(String string)
{
      char[] c = string.toCharArray();
      for(int i=0; i < string.length(); i++)
      {
          if ( !Character.isDigit(c[i]))
          {
             return false;
          }
     }
     return true;
}
Reply With Quote
  #3  
Old December 8th, 2003, 09:41 AM
nydood nydood is offline
Senior eCharchan
 
Join Date: Dec 2001
Posts: 4,345
nydood is a name known to allnydood is a name known to allnydood is a name known to allnydood is a name known to allnydood is a name known to allnydood is a name known to all
I thought IsDigit takes strings as parameters also (dont exactly remember, its been a while since I did any Java), in which case, the for loop will not be needed.
Reply With Quote
  #4  
Old September 9th, 2009, 10:51 PM
echarcha's Avatar
echarcha echarcha is offline
Sutradhar {admin}
 
Join Date: Jul 2000
Location: CA, USA
Posts: 45,295
echarcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond reputeecharcha has a reputation beyond repute
Exclamation Re: Java query : How to find if a String contains only numbers?

Just updating this thread as aragorn was viewing this old thread.

Suppose you want to test for a particular pattern in a String, then this simple class can help.

Code:
public class MatchTester
{
    public static void main(String [] args)
    {
        if (args.length < 2)
        {
            System.err.println("Usage: java MatchTester <string> <pattern>");
            System.exit(0);
        }
 
        try
        {
            boolean doesMatch       = args[0].matches(args[1]);
 
            System.out.println("string : \'" + args[0] + "\'");
            System.out.println("pattern: \'" + args[1] + "\'");
            System.out.println("string does " + (doesMatch ? "" : "not ") + "match pattern");
        }
        catch (Exception e)
        {
            e.printStackTrace(System.err);
        }
    }
}
And here is an example usage to test if the string is a telephone number:

Code:
java MatchTester "123-456-7890" "\d{3}-\d{3}-\d{4}"
string : '123-456-7890'
pattern: '\d{3}-\d{3}-\d{4}'
string does match pattern
__________________
eCharcha.com
-Loud and Proud Desi Opinions
Reply With Quote
  #5  
Old September 10th, 2009, 10:43 AM
aaaj75's Avatar
aaaj75 aaaj75 is offline
Junior eCharchan
 
Join Date: Jan 2009
Location: USA
Posts: 183
aaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond reputeaaaj75 has a reputation beyond repute
Re: Java query : How to find if a String contains only numbers?

Why not just create a Integer/Long/Decimal object. If it throws NumberFormatException it means it is not a valid number..
Reply With Quote
  #6  
Old September 10th, 2009, 10:48 AM
2gud's Avatar
2gud 2gud is offline
We change, values don't
 
Join Date: Nov 2003
Posts: 4,040
2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute2gud has a reputation beyond repute
Re: Java query : How to find if a String contains only numbers?

use regular expression...

[0-9]+

Java provides a regular expression library, refer that...
__________________
If you're doing business with a religious son-of-a-bitch, get it in writing. His word isn't worth shit. Not with the good lord telling him how to fuck you on the deal. - William Burroughs
Reply With Quote
Reply

Bookmarks

Tags
array, char, how to, integer, java, number


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find length of a UTF-8 string in javascript? echarcha How to 0 July 17th, 2008 11:35 AM
How to find image type, size, width and height in Java? echarcha How to 10 January 2nd, 2007 01:15 PM
How to do a Java text string based search for known values? echarcha How to 4 April 15th, 2005 05:22 PM
Another Query.. for Java raniraja Computing 9 May 27th, 2004 09:29 PM
Java help needed - removing newlines from String echarcha SoapBox 5 April 21st, 2003 06:24 PM


All times are GMT -7. The time now is 04:41 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Site Copyright © eCharcha.Com 2000-2012.