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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:12:25+00:00 2026-06-17T17:12:25+00:00

I wrote a program where I want some output which starts with an uppercase

  • 0

I wrote a program where I want some output which starts with an uppercase letter + digit. Then some random generation from the allowed characters.

But I find the output contains spaces like: k r w ea. Whereas I expected : W3krwea.

I don’t know why these spaces appear in my output.

import java.util.Random;
import sun.security.util.Password;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author taskin
 */

    public class RPG
{

    public static String generate(String allowedCharacters,
                                  boolean mustHaveUppercase, boolean mustHaveDigit,
                                  int minLength, int maxLength)
    {
        Random rand=new Random();
        String rp = null; // Random Password
        int Length=allowedCharacters.length();
        char[] password=new char[25];
        int [] asc=new int[18];
        char[] UpperCase=new char[25];
        char[] Digit=new char[25];
        char[] Mixed=new char[25];
        int point1=0,point2=0,point3=0;

        for (int i=0;i<Length;i++)
        {
            asc[i]=(int)allowedCharacters.charAt(i);

        }
        for (int k=0;k<Length;k++)
        {
            if (asc[k]>=65 && asc[k]<=90)
            {
                UpperCase[point1]=(char)asc[k];
                point1++;
                continue;
            }
            else if (asc[k]>=48 && asc[k]<=57)
            {
                Digit[point2]=(char)asc[k];
                point2++;
                continue;
            }
            else
            {
                Mixed[point3]=(char)asc[k];
                point3++;
            }

        }
        StringBuilder strbld=new StringBuilder();
        int length=maxLength-minLength+1+minLength;
        strbld.append(UpperCase[rand.nextInt(UpperCase.length)]);//get a uppercase char
        strbld.append(Digit[rand.nextInt(Digit.length)]);//get a digit
        for(int i=0;i<length-2;i++){
            strbld.append(Mixed[rand.nextInt(Mixed.length)]);
        }
        rp=strbld.toString();

        // Your code to set rp appropriately goes here

        return rp;
    }

    public static void main(String[] args)
    {
        String allowedCharacters = "weakPasSWorD1234$*";
        boolean mustHaveUppercase = true;
        boolean mustHaveDigit = true;
        int minLength = 3;
        int maxLength = 20;
        String randomPassword = generate(allowedCharacters,
                                         mustHaveUppercase, mustHaveDigit, minLength,
                                         maxLength);
        System.out.println("Random password: " + randomPassword);
    }
}
  • 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-17T17:12:26+00:00Added an answer on June 17, 2026 at 5:12 pm

    The .length what you are using, will return the total length of your Char array.Which is around 25 or something.But,in reality,your length is about 4-5 chars.So,it will give rise to some white spaces.Note: the function is not same as .length() (you misunderstood i think. )

    Please look at THIS thread to make the concept clear.

    Use the same length what you have used in your Loops.
    Change the StringBuilder block to :

        StringBuilder strbld=new StringBuilder();
        int length=maxLength-minLength+1+minLength;
        System.out.println("n     "+strbld.append(UpperCase[rand.nextInt(point1)]));//;
    
        //get a uppercase char
        strbld.append(Digit[rand.nextInt(point2)]);
        for(int i=0;i<length-2;i++){
            strbld.append(Mixed[rand.nextInt(point3)]);
    

    Output:

      W1aearere$karasa$akke
    

    No white space.

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

Sidebar

Related Questions

I wrote a program that forks some processes with fork(). I want to kill
I want to write a program which replaces each specific letter by text according
I have a long running program, my_prog that has some output that I want
I want to write a program to hash some data and preferably i would
I want to write a program, which is checks an entered String of the
I've been writing output from perl scripts to files for some time using code
I want to enable my program to output Greek letters. I've got ICU installed
My programs generally generate huge output files (~1 GB) which I do not want
I wrote my own terminal program that reads from the serial port to read
I have a client and a host program (written in c) which I want

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.