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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:40:27+00:00 2026-05-17T21:40:27+00:00

I have a question about validation in Java, I have looked at previous topic

  • 0

I have a question about validation in Java, I have looked at previous topic and none seem to answer my little problem.

What I am trying to do is validate what is put into a string variable in the constructor but also when a set method is used.

What I need to is If Mr Miss Mrs or Ms is entered, then to set that in a Variable (title) if not then to set title as ”Not set” and print an error, now I know how to the the last part, its the the validation of what the user is entering into the variable I am stuck in… I have tried using an array but couldn’t get it to work and tried if statements, again couldn’t get it to function

  • 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-17T21:40:28+00:00Added an answer on May 17, 2026 at 9:40 pm
    public void setTitle(final String title)
    {
      if (title.matches("^Mrs|Mr|Miss|Ms$"))
      {
        this.title = title;
      }
      else
      {
        this.title = "Not Set";
        System.err.format("%s is not a valid title, expecting Mrs,Mr,Miss,Ms\n", title);
      }
    }
    

    if you want to do case insensitive then change the regular expression to:

    "(?i)^Mrs|Mr|Miss|Ms$"
    

    then you can just lowercase the entire thing and uppercase just the first letter to re-normalize your input to what you really want. Google for “java proper case” to find some pre-written snippets of code.

    A more concise one liner, again case sensitive:

    public void setTitle(final String title)
    {
      title.matches("^Mrs|Mr|Miss|Ms$")) ? this.title= title: this.title= "Not Set";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ні, all! I have a little question about jQuery.Validation plugin: - Can I complete
I have a question about how GC works in Java. Consider the following code:
I asked a previous question about serialization and validation. Someone mentioned using the JSON
I have a question about GUI design, specifically with Java Swing and creating clean
I have a question about form validation in CodeIgniter: <? form_open('user/check_login' ?> <fieldset id=login_fields>
I'm new to XML and XSD and I have a question about XML-Schema validation.
I have a quick question about the jQuery.validate.js plugin from bassistance. I'm using version
I am using JQuery Validation plugin and have a question about validation. If I
I have a question about ModelState and validation error messages in MVC3. I have
I have question about parsing in Html helper : I have sth like: @foreach

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.