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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:15:37+00:00 2026-06-03T20:15:37+00:00

I have a string that I load throughout my application, and it changes from

  • 0

I have a string that I load throughout my application, and it changes from numbers to letters and such. I have a simple if statement to see if it contains letters or numbers but, something isn’t quite working correctly. Here is a snippet.

String text = "abc"; 
String number; 

if (text.contains("[a-zA-Z]+") == false && text.length() > 2) {
    number = text; 
}

Although the text variable does contain letters, the condition returns as true. The and && should eval as both conditions having to be true in order to process the number = text;

==============================

Solution:

I was able to solve this by using this following code provided by a comment on this question. All other post are valid as well!

What I used that worked came from the first comment. Although all the example code provided seems to be valid as well!

String text = "abc"; 
String number; 

if (Pattern.matches("[a-zA-Z]+", text) == false && text.length() > 2) {
    number = text; 
}
  • 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-03T20:15:38+00:00Added an answer on June 3, 2026 at 8:15 pm

    If you’ll be processing the number as text, then change:

    if (text.contains("[a-zA-Z]+") == false && text.length() > 2){
    

    to:

    if (text.matches("[0-9]+") && text.length() > 2) {
    

    Instead of checking that the string doesn’t contain alphabetic characters, check to be sure it contains only numerics.

    If you actually want to use the numeric value, use Integer.parseInt() or Double.parseDouble() as others have explained below.


    As a side note, it’s generally considered bad practice to compare boolean values to true or false. Just use if (condition) or if (!condition).

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

Sidebar

Related Questions

I have a Java Properties object that I load from an in-memory String ,
I have a string that contains comma seperated email addresses. I then load this
I have an IConfig object that contains settings used throughout my application. At the
I have very simple XML in a string that I'm trying to load via
I have: String uri = @drawable/myresource.png; How can I load that in ImageView? this.setImageDrawable?
I have a string that contains a known number of double values. What's the
I have a string that contains a czech character. the string is 0bálka This
I have a string that contains several placeholders in it, and each placeholder is
Basically, I have a string of functions that load a set of blog entries
i have a string that looks like this: /dir/location/test-load-ABCD.p and i need to parse

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.