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

  • Home
  • SEARCH
  • 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 8722137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:20:33+00:00 2026-06-13T07:20:33+00:00

I want to write a simple java program to check whether keycode matches a

  • 0

I want to write a simple java program to check whether keycode matches a set of conditions.

This is what I have so far:

Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();

do {

    if (key1.length() < 6 || key1.length() > 8) {

        System.out.println("must be at least 6 letter and max 8 letter");
        return;
    }

    else {

        boolean upper = false;
        boolean lower = false;
        boolean number = false;

        for (char c : key1.toCharArray()) {
            if (Character.isUpperCase(c)) {
                upper = true;
            } else if (Character.isLowerCase(c)) {
                lower = true;
            } else if (Character.isDigit(c)) {
                number = true;
            }
        }
        if (!upper) {
            System.out.println("must contain at least one uppercase character");
            return;
        } else if (!lower) {
            System.out.println("must contain at least one lowercase character");
            return;
        } else if (!number) {
            System.out.println("must contain at least one number");
            return;
        } else {
            return;
        }
    }

} while (true);

System.out.println("Input keycode again");
String key2 = keycode.nextLine();

if (key1.equals(key2)) {

    System.out.println("keycode matched");
} else {
    System.out.println("keycode dont match");

}

The user is prompted to input a keycode.

The program first checks if it is more than 6 characters and less than 8 characters.

It then checks if it contains a lowercase, uppercase and a number.

I want it to allow user to input the password again if he were to make any mistake rather than doing all over again.

If successful, it will ask the user to input keycode again. If the two keycodes don’t match,
the user is allowed to try again. After 3 unsuccessful tries, the system will reply with ‘keycode mismatch’.

The part I need assistance in is allowing user to input password if it doesn’t fit the requirement and password mismatch. When I enter a password less than 6 characters, I get the following output:

must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
  • 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-13T07:20:34+00:00Added an answer on June 13, 2026 at 7:20 am

    Use a boolean variable say redo. And at every place you have used return, replace it with redo = true. And your while condition should be while(redo), which will run until redo is true, that it, user hasn’t entered correct number.

    Also, move your input reading line: –

    String key1 = keycode.nextLine();
    

    Inside your do-while loop. And also ,reset your redo variable to false in the loop, so that it doesn’t remain true forever.

    String[] keys = new String[2];  // Since you are reading 2 keys, so size = 2
    boolean redo = false;
    int count = 0;    // To maintain the number of keys read. should stop at count = 2
    
    do {
    
        redo = false;
        Scanner keycode =  new Scanner(System.in);
    
        System.out.println("Input keycode No: + " + (count + 1));
        String key1 = keycode.nextLine();
    
        if (key1.length() < 6 || key1.length() > 8) {
            redo = true;
    
        } else {
            /** Your remaining code **/
    
            if (!upper) {
                System.out.println("must contain at least one uppercase character");
                redo = true;
    
            } else if (!lower) {
                System.out.println("must contain at least one lowercase character");
                redo = true;
    
            } else if (!number) {
                System.out.println("must contain at least one number");
                redo = true;
    
            } else {
                keys[count++] = key1;  // Enter a new key in array
            }
        }
    
    } while (redo || count < 2);  
    
    if (keys[0].equals(keys[1])) {
        System.out.println("Keys are equal");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple visualization of a Java program by displaying the
I want to write a simple java program to read in a text file
i am trying to run a very simple java program. i want to write
I have been asked to write a (very) simple program for a set of
I'm new to Java programming. I want to write simple Java network protocol. I
I want to write a simple java agent which can print the name of
I want to write a simple c program to do the following. Open a
I want to write a very simple game in Java to demonstrate a wireless
I want to build a simple consumer program (in java) to get all messages
Basically, I just need to write a simple java program to detect the version

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.