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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:14:59+00:00 2026-06-14T00:14:59+00:00

I am trying to determine whether a string contains a positive int or not.

  • 0

I am trying to determine whether a string contains a positive int or not. My code is:

public void isInt(String str) throws NotIntException{
    String integer=str.replaceAll("\\d","");
    System.out.println(integer);
    if (!integer.equals("")){
        throw new NotIntException("Wrong data type-check fields where an integer"+
        " should be.");
    }//end if
    if (integer.equals("-")){
        System.out.println(integer);
        throw new NotIntException("Error-Can't have a negative count.");
    }//end if
}//end method

I am testing this with a string “-1”, which should, after the replaceAll(), become “-“. This should enter both if-statements. But it only enters the first. I tried it with the == comparison too, just in case, but it didn’t work either. What’s odd to me is that whether I look to fulfill the second if-statement’s condition or to fulfill its negation [i.e., !integer.equals(“-“)], the program still doesn’t enter the if….

Thanks, usually my comparison issues are just me missing something basic, but I really don’t see anything here…

  • 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-14T00:15:00+00:00Added an answer on June 14, 2026 at 12:15 am

    Since you are throwing an Exception in your first if, so, your 2nd if will not even be tested.

    if (!integer.equals("")){
        throw new NotIntException("Wrong data type-check fields where an integer"+
        " should be.");
    }
    
    if (integer.equals("-")){
        System.out.println(integer);
        throw new NotIntException("Error-Can't have a negative count.");
    }
    

    If your code enters the first if, it will not execute further.


    But, why are you using this approach for your problem.

    You can easily use Integer.parseInt to check for valid integer. And then if it is valid, then test whether its less than 0. It would be far easier and readable.

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

Sidebar

Related Questions

I'm trying to determine whether or not a string contains a number. This doesn't
I am trying to determine whether a string contains exactly one given distinct character,
I am trying to determine whether a string input by a user is valid
I'm trying to determine whether or not two strings match, and even though when
I have written the following code where I am trying to determine whether a
I'm trying to write a function that is able to determine whether a string
I'm trying to diff two strings to determine whether or not they solely vary
I'm trying to determine whether or not a expression passed into my Expressions class
I'm trying to determine whether or not the user has voted on a certain
I am trying to write Prolog code to determine whether the bound variable X

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.