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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:27:50+00:00 2026-06-12T22:27:50+00:00

First off, sorry the code is kinda messy. It didn’t format very well. My

  • 0

First off, sorry the code is kinda messy. It didn’t format very well. My assignment is to randomly generate a password consisting of uppercase and lowercase letters, with the length being specified by the user. So far, my code is unfinished but that’s because I’ve run into a problem.

My password will display lowercase and uppercase letters, but it won’t be long enough.

For example, if the person wants a password with a length of 14, I’ll only get a password that’s less than that. It’s never the length that it’s supposed to be.

import java.util.Scanner;
import java.util.Random;

public class Password{
    public static void main(String [] args){
        Scanner in = new Scanner(System.in);
        int passwordLength = 0;
        Random randNum = new Random();
        int randNumAscii = 0;
        String generatedPassword = "";

        System.out.print("Password Length (1-14): ");
        passwordLength = in.nextInt();
        for(int count = 0; count < passwordLength; count++){ 
            randNumAscii = randNum.nextInt(123);
            if(randNumAscii >= 65 && randNumAscii <= 90 || randNumAscii >= 97 && randNumAscii <= 122)
                generatedPassword += (char)randNumAscii;
            else
                randNumAscii = randNum.nextInt(123); 
        }
        System.out.println(generatedPassword);
    }
}
  • 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-12T22:27:52+00:00Added an answer on June 12, 2026 at 10:27 pm

    It’s because you only add a character to the generated password if it’s in your selected range. If a character is randomized outside of randNumAscii >= 65 && randNumAscii <= 90 || randNumAscii >= 97 && randNumAscii <= 122 you never append it to the string.

    What you can do is either have a while loop inside the for loop that continues until that condition is met, or continue the for loop but decrease the count variable with one so a new character is generated “with that index” so to speak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm sorry if the title doesn't explain this very well. I'm looking
First off, sorry if this is a stupid question. I installed code::blocks to try
Let me first start off, sorry for the confusing title. I didn't know how
First off - sorry for the wall of code but it's not too horrendous,
First off, sorry for what I know is terrible idiomatic Ruby code. I am
First off i am super sorry that i have no code for this. That
First off, I'm sorry if this shows no code which is not what Stack
First off, I'm sorry for asking such a simple question in such a sophisticated
First off I use this code to make the navigation bar always stay fixed;
Hey guys, first off all sorry, i can't login using my yahoo provider. anyways

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.