File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check if String() value is numeric

 
Winston Gutkowski
Bartender
Pie
Posts: 9461
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Eclipse IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iterate the string using charAt(index) and for each char ch:
Character.isDigit(ch)
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? Why is that?
 
vedant basu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I was referring more to the fact that the inequalities are completely backwards.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, this code has many problems.

In comparison, Character.isDigit() simply works.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20170
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic