Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:12:22+00:00 2026-06-06T04:12:22+00:00

Though the question is generic, I would mention the scenario which sparked the query.

  • 0

Though the question is generic, I would mention the scenario which sparked the query.

Scenario:

I am interested in analyzing a large number of strings (numeric ones in particular). Therefore, my first job is to filter out those ones which contain even a single character other than numbers.

A simple way to do this is (in Java):

for (String val : stringArray){
   try{
     int num = Integer.parseInt(val);
     doSomething(num);
   }
   catch(NumberFormatException nfe){}
}

Another point which I must mention is that there are only about 5% of the strings in the array which are purely numeric. Thus there would be, in short, a lot of catching involved.

What I was wondering about was that whether this was an efficient way in terms of design or should I be thinking of other ways to do the same?


Conclusion based on answers: Exceptions are indeed expensive and it is not a very good design practice to use them as a form of control statement.
Therefore, one should try and look for alternatives wherever possible and if still exceptions seem to be clearer/easier, one should document it well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:12:25+00:00Added an answer on June 6, 2026 at 4:12 am

    What you do here is inherently correct as there is no other standard way in java to check if a string is numeric.

    If a profiling proves you that this operation is too long, you could try to do it yourself as in the parseInt method but the JVM won’t be able to do the same optimizations so I don’t recommend it. You’ll see that the JVM is heavily optimized to handle exceptions and that it does this job very well.

    As a curiosity, here are a few ways to do it in java :

    http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Java

    with links to other languages, but your solution is the standard and idiomatic one and I doubt you’ll find a big difference by rewriting it as in the example :

    private static final boolean isNumeric(final String s) {
      if (s == null || s.isEmpty()) return false;
      for (int x = 0; x < s.length(); x++) {
        final char c = s.charAt(x);
        if (x == 0 && (c == '-')) continue;  // negative
        if ((c >= '0') && (c <= '9')) continue;  // 0 - 9
        return false; // invalid
      }
      return true; // valid
    }
    

    Using this, in my opinion, would be a typical case of premature optimization leading to a less maintainable code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Django's comment framework which utilizes generic foreign keys. Question: How do
This is sort of a generic Good Idea/Bad Idea question. My scenario: I am
This question is generic, and I would simply like to know how to dump
This may be a simple question though can´t figure out how to do it.
I am familiar with MVC/MVP though my question is simple, I'm about to program
I have tried looking though several of the already asked question about this topic
I feel as though this this is a simple question, but can't find an
though I have visited this site many times, this is my first question. After
I feel as though this will end up being a silly question with a
I have an easy question, though I'm not sure if it's doable or not:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.