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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:43:00+00:00 2026-06-03T21:43:00+00:00

What I would like to do: restrict user input to letters only (lowercase and

  • 0

What I would like to do:

  • restrict user input to letters only (lowercase and caps)
  • error message on incorrect input
  • loop until correct input

Quite a few sites with similar questions suggested regular expressions, patterns, and matchers. I had at look at the API, and it confused me a lot…

Here’s what I tried.

public class restrictinput {
    public static void main (String args [] ) {
        Scanner sc = new Scanner (in);
        System.out.println ("Enter  blah ");
        String blah = sc.nextLine();
        Pattern userInput = Pattern.compile("^[a-zA-Z]+$");
        Matcher inputCheck = userInput.matcher("blah");
    }
}

This compiles, but I’m not sure if it’s the correct/best way to do it. But, if I enter a different character type, it just executes the rest of the code.

How do I only make it execute if the correct character type is received, and what should I use to make the error known to the user?

If the code given is wrong, what should I do to change it?

  • 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-03T21:43:01+00:00Added an answer on June 3, 2026 at 9:43 pm

    Okay so there are a couple things here you need to fix. The first thing I noticed is that you want

    userInput.matcher(blah);
    

    not

    userInput.matcher("blah");
    

    because you are matching the string, not just "blah"

    Now for the bulk of your question. The first thing you need is to look at the Matcher object. Specifically look into Matcher.find().

    The second thing is that you need to add some sort of a conditional loop to your code so that it keeps asking for input. Maybe something like:

    bool result = true;
    do {
        String blah = sc.nextLine();
        Pattern userInput = Pattern.compile("^[a-zA-Z]+$");
        Matcher inputCheck = userInput.matcher("blah");
        result = //boolean check from your matcher object
        if(result) {
            //Complain about wrong input
        }
    } while(result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to validate and restrict newline/linefeed character. Valid XML: <root> <node>data data</node>
I have a web folder that I would like to restrict access to via
My web application contains a text box for which I would like to restrict
In an ASP.NET WebForms application I would like to allow the end-user to browse
Background: I would like to automatically detect the language written by the user in
I would like to set up a home page where pressing any character (lowercase
I'm using Jqueryui connected sortable lists and I would like to drop elements only
Like the title says, I'm looking for a way to restrict user creation of
I would like to restrict the fields while creating a new post in WordPress.
I would like to create a custom annotation to decorate methods which would restrict

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.