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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:41:48+00:00 2026-06-02T16:41:48+00:00

I am trying to do an assignment for class, but I have run into

  • 0

I am trying to do an assignment for class, but I have run into a problem that I cannot find the solution for. I have a variable in my main method called passwd. I have a user enter a possible password and that input is stored in the variable. I then check the length of the password variable to make sure it meets the length requirements. I want to then have another method chat checks each of the characters of the variable to see if it is a digit.

The problem is I cannot use the passwd variable from the main method in my digitCheck() method.

Can someone please advise me on how to solve this problem.

 package Password;

import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author Curtis
 */
public class Password 
{
private static Component frame;

//Main Method
public static void main(String[] args) 
{//Declaration of variables
    String passwd;
    int leng;
    boolean length = false;
    //Prompt user to enter possible password
    while(!length)
    {
        passwd = JOptionPane.showInputDialog("Please enter a possible password:\n" +  
            "Password must contain 6-10 characters\n"+
            "Password must contain both a letter and a digit");
        leng =passwd.length();//Determines Password Length
        if(leng>5 && leng<11)
            { 
                length = true;
                digitCheck();
            }
        else //Gives Password Length Error Message
        {
            length = false;
            JOptionPane.showMessageDialog(frame, "Your password does not meet the length requirements.", "Password Length Error", JOptionPane.ERROR_MESSAGE); 
        }
    }
}
//Digit Check Method
public static void digitCheck();
{// declaration of variables
    char c;
    int digits = 0;
    for(int i=0;i<leng;i++)
    {
        c = passwd.charAt(i);
        if(Character.isDigit(c))
            digits++;
    }

}

}
  • 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-02T16:41:50+00:00Added an answer on June 2, 2026 at 4:41 pm

    The problem in your case is because you are declaring the variable within different scopes. You have two options:

    Either:

    • Move your variable to a Global Variable, you do this by declaring the variable outside a method, usually just under the class decleration, in your case this could be just above or below private static Component frame;.

    • Pass your variable as a parameter to the other method, so you would need to change your digitCheck() method to digitCheck(String passwd) and then call it like so: digitCheck(passwd).

    The first option will allow you access the variable from what ever section of your class you are into. On the other hand, the second option will allow you to easier re-use the method you have created since the method will be self contained, not relying on the usage of global variables.

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

Sidebar

Related Questions

I am trying to use the stdlib stack in a class that I have
I have a class that contains a few multi-dimensional arrays. I am trying to
I have an assignment about fftw and I was trying to write a small
I am trying to learn Java's threads in order to do an assignment, but
I have a small assignment in C. I am trying to create an array
first off this is a class assignment so i would appreciate help but just
I have one class that declares an enumeration type as: public enum HOME_LOAN_TERMS {FIFTEEN_YEAR,
I am trying to write public instance method createParther() that returns an instance of
I'm taking a beginning Java class and an assignment requires that I write a
I am working on an assignment with polymorphism and have followed online tutorials that

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.