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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:38:02+00:00 2026-05-27T07:38:02+00:00

I am trying to figure out how to check a String to validate whether

  • 0

I am trying to figure out how to check a String to validate whether it had at least one letter and one number in it. I will be upfront that this is homework and I am a little confused.

There is a method isLetterOrDigit() method that seems it would be the right approach, but I am undure as how I would implement this in my code. Here is the code I am using below:

import javax.swing.JOptionPane;

public class Password
{
    public static void main(String[] args)
    {

    String initialPassword;
    String secondaryPassword;
    int initialLength;

    initialPassword = JOptionPane.showInputDialog(null, "Enter Your Passowrd.");

    initialLength = initialPassword.length();

    JOptionPane.showMessageDialog(null, "initialLength = " + initialLength);

    while (initialLength < 6 || initialLength > 10)
    {
        initialPassword = JOptionPane.showInputDialog(null, "Your password does not meet the length requirements. It must be at least 6 characters long but no longer than 10.");
        initialLength = initialPassword.length();
    }

    //Needs to contain at least one letter and one digit

    secondaryPassword = JOptionPane.showInputDialog(null, "Please enter your password again to verify.");

    JOptionPane.showMessageDialog(null, "Initial password : " + initialPassword + "\nSecondar Password : " + secondaryPassword);

    while (!secondaryPassword.equals(initialPassword))
    {
        secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again."); 
    }

    JOptionPane.showMessageDialog(null, "The program has successfully completed."); 

    }
}

I want to implement a method where the comment section is using either the isDigit(), isLetter(), or isLetterOrDigit() methods, but I just don’t know how to do it.

Any guidance would be appreciated. Thanks in advance for the assistance.

  • 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-27T07:38:03+00:00Added an answer on May 27, 2026 at 7:38 am

    This should work.

    public boolean containsBothNumbersAndLetters(String password) {
      boolean digitFound = false;
      boolean letterFound = false;
      for (char ch : password.toCharArray()) {
        if (Character.isDigit(ch)) {
          digitFound = true;
        }
        if (Character.isLetter(ch)) {
          letterFound = true;
        }
        if (digitFound && letterFound) {
          // as soon as we got both a digit and a letter return true
          return true;
        }
      }
      // if not true after traversing through the entire string, return false
      return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to check if a string matches a regular
Trying to figure out how to write a jquery formula that will sum all
I'm trying to figure out how I'd add a contains to check that the
I'm trying to write a regular expression that will check so a given string
Check this link! I am having the hardest time trying to figure out why
After spending hours I still cant figure it out. I'm trying to validate an
I'm trying to figure out the best way to search a number of different
I am stuck trying to figure out how to do string hashing with linear
I am trying to figure out how to take a comma-separated string as input
I'm trying to figure out a way to count the number of characters in

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.