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 7564585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:56:32+00:00 2026-05-30T13:56:32+00:00

if I want to convert a string into an int in java do you

  • 0

if I want to convert a string into an int in java
do you know if there is a way for me to detect overflow?
by that I mean the string literal actually represents a value which is larger than MAX_INT?

java doc didn’t mention it..
it just says that if the string can not be parsed as an integer, it will through FormatException
didn’t mention a word about overflow..

  • 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-05-30T13:56:33+00:00Added an answer on May 30, 2026 at 1:56 pm

    If I want to convert a string into an int in java do you know if there is a way for me to detect overflow?

    Yes. Catching parse exceptions would be the correct approach, but the difficulty here is that Integer.parseInt(String s) throws a NumberFormatException for any parse error, including overflow. You can verify by looking at the Java source code in the JDK’s src.zip file. Luckily, there exists a constructor BigInteger(String s) that will throw identical parse exceptions, except for range limitation ones, because BigIntegers have no bounds. We can use this knowledge to trap the overflow case:

    /**
     * Provides the same functionality as Integer.parseInt(String s), but throws
     * a custom exception for out-of-range inputs.
     */
    int parseIntWithOverflow(String s) throws Exception {
        int result = 0;
        try {
            result = Integer.parseInt(s);
        } catch (Exception e) {
            try {
                new BigInteger(s);
            } catch (Exception e1) {
                throw e; // re-throw, this was a formatting problem
            }
            // We're here iff s represents a valid integer that's outside
            // of java.lang.Integer range. Consider using custom exception type.
            throw new NumberFormatException("Input is outside of Integer range!");
        }
        // the input parsed no problem
        return result;
    }
    

    If you really need to customize this for only inputs exceeding Integer.MAX_VALUE, you can do that just before throwing the custom exception, by using @Sergej’s suggestion. If above is overkill and you don’t need to isolate the overflow case, just suppress the exception by catching it:

    int result = 0;
    try {
        result = Integer.parseInt(s);
    } catch (NumberFormatException e) {
        // act accordingly
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert a string into a series of Keycodes, so that I
How can I convert a String array into an int array in java? I
I want to convert a string into a double and after doing some math
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to convert all texts in a string into html entities but preserving
I want to convert a C-style string into a byte-vector. A working solution would
i am trying to convert a LPCSTR string into LPCTSTR string. i want to
I want to convert an OutputStream into a String object. I am having an
I am using the below function in Java to convert an encrypted String into
I want to convert a String into KeyEvent to do something like this :

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.