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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:19:05+00:00 2026-05-15T14:19:05+00:00

Basically, my situation requires me to check to see if the String that is

  • 0

Basically, my situation requires me to check to see if the String that is defined by user input from the keyboard is only alphabetical characters in one case and only digits in another case. This is written in Java.

my current code:

switch (studentMenu) {
                case 1: // Change all four fields
                    System.out.println("Please enter in a first name: ");

                    String firstNameIntermediate = scan.next();
                    firstName = firstNameIntermediate.substring(0,1).toUpperCase() + firstNameIntermediate.substring(1);
                    System.out.println("Please enter in a middle name");
                    middleName = scan.next();
                    System.out.println("Please enter in a last name");
                    lastName = scan.next();
                    System.out.println("Please enter in an eight digit student ID number");
                    changeID();
                    break;
                case 2: // Change first name
                    System.out.println("Please enter in a first name: ");
                    firstName = scan.next();
                    break;
                case 3: // Change middle name
                    System.out.println("Please enter in a middle name");
                    middleName = scan.next();
                    break;
                case 4: // Change last name
                    System.out.println("Please enter in a last name");
                    lastName = scan.next();
                case 5: // Change student ID:
                    changeID();
                    break;
                case 6: // Exit to main menu
                    menuExit = true;
                default:
                    System.out.println("Please enter a number from 1 to 6");
                    break;
            }
        }
    }

public void changeID() {
    studentID = scan.next();
    }

I need to make sure the StudentID is only numerical and each of the name segments are alphabetical.

  • 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-15T14:19:05+00:00Added an answer on May 15, 2026 at 2:19 pm

    java.util.Scanner can already check if the next token is of a given pattern/type with the hasNextXXX methods.

    Here’s an example of using boolean hasNext(String pattern) to validate that the next token consists of only letters, using the regular expression [A-Za-z]+:

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter letters:");
        while (!sc.hasNext("[A-Za-z]+")) {
            System.out.println("Nope, that's not it!");
            sc.next();
        }
        String word = sc.next();
        System.out.println("Thank you! Got " + word);
    

    Here’s an example session:

    Please enter letters:
    &#@#$
    Nope, that's not it!
    123
    Nope, that's not it!
    james bond
    Thank you! Got james

    To validate that the next token is a number that you can convert to int, use hasNextInt() and then nextInt().

    Related questions

    • Validating input using java.util.Scanner – has many examples!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Situation I have a table in a DB that contains job types, basically
I have a situation that requires two SQL Compact edition databases to synchronise with
Basically I have some variables that I don't want to preinitialize: originalTime = None
basically i have (state, state code) pairs, that are subsets of country [USA] ->
Basically what I need to do is write a function that takes in a
I'm currently working on a project that requires me to match our database of
I'm in a situation where I've got a project that has a large number
So I've been working on a project that requires offering the possibility to view
Basically the situation is like this (simplified, pseudo code): I have 2 TABLES person
For the full story, check out my other question . Basically, I had asked

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.