GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How to check if String() value is numeric Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to check if String() value is numeric" Watch "How to check if String() value is numeric" New topic
Author

How to check if String() value is numeric

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7896
    
  21

Campbell Ritchie wrote:Do you mean like the ATTACHED type in Eiffel?

Quite possibly. Unfortunately, Eiffel is one of those languages that I've heard a lot about but never used.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
Eiffel? I once tried to write a compiler for it.
It used to be really popular, particularly in the late 1980s and 1990s. A really good language for learning object‑orientation (OO). Bertrand Meyer plugged it as a reliable language; it has keywords which cause Exceptions to be thrown if a class invariant is breached, or a loop variant doesn’t alter, etc. It is fully OO, even things like INTEGER being full‑blown objects. Unfortunately it has a context‑sensitive grammar, and about 2005 there were major changes to the language specification and many of its supporters deserted it. If you look at old Tiobe indices, you can watch it fall gradually from 15th position to the limbo of “not graded because the differences are too slight” in the >50 category.
Vaishali S Kulkarni
Greenhorn

Joined: Oct 05, 2011
Posts: 6

iterate the string using charAt(index) and for each char ch:
Character.isDigit(ch)


Thanks,
Vaishali
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Vaishali Kulkarni - Boston wrote:iterate the string using charAt(index) and for each char ch:
Character.isDigit(ch)

Yes, that was the first thing Campbell suggested in the very first response.

Seven years ago.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
I prefer to use the scanner method if you are using the command line method

If on the other hand if you are using GUI then the parse method would be bettered used

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Really? Why is that?
vedant basu
Greenhorn

Joined: Sep 11, 2012
Posts: 6
change it to lowercase, then extract each character.
check the unicode(or ascii, i'm not sure which, but i think its unicode) of each character, to see if it falls in the range 48-57.
this can be done by
char c=string.charAt(1);//example
int x=c;
if(x<=48)&&(x>=57)
{
counter++;
}
if the counter=string.length(), its a number, as each character is a number
I hope this helps!
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Um, yes. I think that's been suggested a few times now. Except this version has some fatal bugs; it won't count anything.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
Why are you changing the char to an int? Also using the number literals 48 and 57 is error‑prone. You should use char literals.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, I was referring more to the fact that the inequalities are completely backwards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
I wasn’t referring to the >= etc; that is a different error which will definitely cause problems. There is another feature about that line which I noticed and have kept quiet about, but which the compiler won’t keep quiet about.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Yeah, this code has many problems.

In comparison, Character.isDigit() simply works.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
Mike Simmons wrote: . . . Character.isDigit() . . .
I had forgotten about that. Even though I would appear to have been the first person to mention it on this discussion!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Mike Simmons wrote:In comparison, Character.isDigit() simply works.


And not only does it Just Work, it works better because it identifies "٢" as a digit. It doesn't restrict itself to just Latin digits, in other words.

(In case you don't recognize that character, it's the Arabic digit 2.)

 
GeeCON Prague 2014
 
subject: How to check if String() value is numeric