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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:36:38+00:00 2026-06-18T00:36:38+00:00

Hey I’m creating a password validation program that requires the program to check for

  • 0

Hey I’m creating a password validation program that requires the program to check for digits, letters, and the length of the password, then the program compares the two passwords to see if they match. Everything works fine, but when the error message displays, it shows multiple message boxes. I know this has something to do with the for loop, but I’m still a beginner and I have no idea how to fix it.

Here’s my code:

import javax.swing.JOptionPane;

public class Passwords
{
public static void main(String[] args)
{
    String passOne, passTwo;
    passOne = JOptionPane.showInputDialog(null, "Please enter a password");
    passTwo = JOptionPane.showInputDialog(null, "Please re-enter your password");




    //Loop for each digit in password
    for(int x = 0; x < passOne.length(); x++)
    {
        //Testing for a digit
        if(Character.isDigit(passOne.charAt(x)))
            {
                //Testing for a letter
                if(Character.isLetter(passOne.charAt(x)))
                {
                    //Testing length 6-10 chars
                    if(passOne.length() <= 10 && passOne.length() >= 6)
                    {
                        //comparing two passwords
                        if(passOne.equals(passTwo))
                        {
                            JOptionPane.showMessageDialog(null, "Contratulations, you have a new password!");
                        }
                        //If passwords don't match
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Passwords do not match, please try again.");
                        }
                    }
                    //If length is wrong
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Password must be between 6 and 10 characters long.");
                    }
                }
                //If no letter
                else
                {

                    JOptionPane.showMessageDialog(null, "Password must contain at least one letter.");
                }

            }
        //If no digit
        else
            {

                JOptionPane.showMessageDialog(null, "Password must contain at least one digit.");           
            }


    }


}
}
  • 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-18T00:36:39+00:00Added an answer on June 18, 2026 at 12:36 am

    It’s because you are showing the error for each character that makes it fail validation.

    The gist of the solution is you have to break out of your loop as soon as you find an error. Then if you did indeed have an error (which you recorded before breaking out), you display it.

    I didn’t actually run it, but it should be closer, try this:

        String error = null;
        // Loop for each digit in password
        for (int x = 0; x < passOne.length(); x++) {
            // Testing for a digit
            if (Character.isDigit(passOne.charAt(x))) {
                // Testing for a letter
                if (Character.isLetter(passOne.charAt(x))) {
                    // Testing length 6-10 chars
                    if (passOne.length() <= 10 && passOne.length() >= 6) {
                        // comparing two passwords
                        if (passOne.equals(passTwo)) {
                            error = "Contratulations, you have a new password!";
                        }
                        // If passwords don't match
                        else {
                            error = "Passwords do not match, please try again.";
                        }
                    }
                    // If length is wrong
                    else {
                        error = "Password must be between 6 and 10 characters long.";
                    }
                }
                // If no letter
                else {
    
                    error = "Password must contain at least one letter.";
                }
    
            }
            // If no digit
            else {
                error = "Password must contain at least one digit.";
            }
    
            if(error != null) break;
    
        }
    
        if(error != null) {
            JOptionPane.showMessageDialog(null, error);           
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey i have made a countdown, but what i want is a div that
Hey guys so in JQuery ui it produces a close link that once clicked
Hey guys i have a problem. There is an application that i built using
Hey guys I'm trying to join together two queries and am having an issue,
Hey guys take a look at this program. /* The craps game, KN king
Hey guys I have an absolute position for an element that will be created
Hey bloggers out there! I've created Wordpress blog that I am hosting myself, and
Hey Folks i am trying to compile this C++ program: #include <stdio.h> #include <string.h>
Hey, I'm looking to find a good MySQL framework for Cocoa that I can
Hey there, I'm writing a python plugin for a geo referencing program called QGIS

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.