[英]Java passing information to constructor
I have a working program here, it takes user input (a password) and checks conditions (is it 8 chars, contains one uppercase, one lowercase, and a digit) then writes the information to a file. 我在这里有一个工作程序,它需要用户输入(密码)并检查条件(是否为8个字符,包含一个大写字母,一个小写字母和一个数字),然后将信息写入文件。
It compiles and runs, but upon further review of the instructions for the assignment, I must use an object to call the methods. 它可以编译并运行,但是在进一步查看赋值指令之后,我必须使用一个对象来调用方法。 This is throwing me off.
这让我失望了。 I think I would use a statement like: password.PasswordCheck(pwd);
我想我会使用如下语句:password.PasswordCheck(pwd); but I am not sure how this would work with what I have or where to put.
但我不确定这与我现有的东西或放置位置如何配合。
Currently my method toString is in charge of building the string to display the info to the user and does it by calling the method acceptable. 当前,我的方法toString负责构建字符串以向用户显示信息,并通过调用可接受的方法来完成此操作。
What must I do to change this so that everything is using the object to call? 我该怎么做才能更改此设置,以便一切都使用该对象进行调用?
Thanks so much! 非常感谢!
import java.util.Scanner;
import java.io.*;
public class PasswordCheck //create the class
{
private boolean isEightCharacters; //true if has 6 characters, false if doesn't
private boolean hasOneUppercase; //true if has 1 uppercase character, false if doesn't
private boolean hasOneLowercase; //true if has 1 lowercase character, false if doesn't
private boolean hasOneDigit; //true if has 1 digit, false if doesn't
public boolean isPasswordValid; //true if all other conditions are true, false otherwise
private String password; //global variable declaring string
//create default constructor
public PasswordCheck(){
isEightCharacters = false; //default value
hasOneUppercase = false; //default value
hasOneLowercase = false; //default value
hasOneDigit = false; //default value
isPasswordValid = false; //default value
}
//create constructor with string as input
public PasswordCheck(String pwd){
password = pwd; //set password to the variable being passed in
setEightCharacters(); //call the method
setOneUppercase(); //call the method
setOneLowercase(); //call the method
setOneDigit(); //call the method
setPasswordValid(); //call the method
}
//create methods to change attributes/fields above
public void setEightCharacters(){
if (password.length() >= 8) //if statement to validate password length
{
isEightCharacters = true; //if passes condition test, set to true
}
else //if not 8 characters
{
isEightCharacters = false; //set boolean to false
}
}
public void setOneUppercase(){ //method to check if password contains uppercase letter
int i; //declare count variable
char ch; //declare char variable
boolean hasUpper = false; //declare and initialize boolean to false
for ( i = 0; i < password.length(); i++ ) { //begin loop to read through string
ch = password.charAt(i); //sets ch to character in position of string
if (Character.isUpperCase(ch)) //tests to see if character is uppercase
{
hasUpper = true; //sets boolean to true
}
}
if(hasUpper == true)
{
hasOneUppercase = true;
}
else
{
hasOneUppercase = false;
}
}
public void setOneLowercase(){ //method to check if password contains lowercase letter
int i; //declare count variable
char ch; //declare char variable
boolean hasLower = false; //declare and initialize boolean
for ( i = 0; i < password.length(); i++ ) { //begin loop
ch = password.charAt(i);
if (Character.isLowerCase(ch)) //tests to see if string contains lowercase letter
{
hasLower = true;
}
}
if(hasLower == true)
{
hasOneLowercase = true;
}
else
{
hasOneLowercase = false;
}
}
public void setOneDigit(){ //method to see if string contains a number
int i; //declares the count variable
char ch; //declares the char variable
boolean hasNumber = false; //declares and intializes the boolean variable
for ( i = 0; i < password.length(); i++ ) { //begin loop
ch = password.charAt(i);
if (Character.isDigit(ch)) //tests to see if character is a number
{
hasNumber = true;
}
}
if(hasNumber == true)
{
hasOneDigit = true;
}
else
{
hasOneDigit = false;
}
}
public void setPasswordValid(){ //method to test is all conditions are met for a valid password
if (isEightCharacters && hasOneUppercase && hasOneLowercase && hasOneDigit == true) //if all true
{
isPasswordValid = true;
}
else
{
isPasswordValid = false;
}
}
//create a method to check ok or missing, pass in one of the booleans
public String acceptable(boolean x) {
String var="";
if (x == true)
var = "OK";
else
var = "Missing";
return var; //return statement
}
//create a method to validate all conditions
public String validate(boolean x) {
String valid="";
if (x == true)
valid = "\nPassword is valid.";
else
valid = "\nPassword is not valid.";
return valid; //return statement
}
// toString creates a string with the output
public String toString(){
String stringToReturn=""; //declare the string to return
stringToReturn += validate(isPasswordValid)+
"\n\n8 characters: "+ acceptable(isEightCharacters)+ "\n\n1 uppercase: "+ acceptable(hasOneUppercase)
+"\n\n1 lowercase: "+acceptable(hasOneLowercase)+"\n\n1 digit: "+acceptable(hasOneDigit);
return stringToReturn;
}
public static void main(String[] args) throws IOException
{
//variables
String filename = "passwordCheck.txt";
String pwd;
//Welcome message
System.out.println("Welcome to the Password Check Program!\n");
//Create scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
//prompt user for the password
System.out.println("Please enter the password to verify: ");
pwd = keyboard.nextLine(); //stores the input
//create object using String as password
PasswordCheck password=new PasswordCheck(pwd); //remember to change PasswordCheck based upon new name of class/filename
//display the validated information
System.out.println(password); //uses toString() method for display
//make sure file does not exist
File file = new File(filename);
if (file.exists())
{
System.out.println("\nFile " + filename + " already exists. Output not written.");
System.exit(0);
}
//Open the file
PrintWriter outputFile = new PrintWriter(file);
//Write the password to the file
outputFile.println(password);
//Close the file
outputFile.close();
System.out.println("\nOutput written to " + filename);
}
}
From my understanding of your question, I think you need to use object's reference variable to call the methods in it. 根据我对问题的理解,我认为您需要使用对象的引用变量来调用其中的方法。 So instead of calling all your methods inside the constructor, call them in your main method as shown below.
因此,与其在构造函数内部调用所有方法,不如在您的main方法中调用它们,如下所示。
More info: By not calling all methods inside a constructor, you have the freedom of calling only those methods that you want to, in your main method. 更多信息:通过不调用构造函数内部的所有方法,您可以自由地在主方法中仅调用所需的那些方法。 For example in any application you want your password to be at least 8 characters long and not necessarily have a digit, lowercase/uppercase alphabet then you can just call one required method.
例如,在任何应用程序中,如果您希望密码长度至少为8个字符,并且不一定要包含数字,小写/大写字母,则只需调用一种必需的方法即可。
Let the constructor be like this 让构造器像这样
public PasswordCheck(String pwd){
password = pwd; //set password to the variable being passed in
}
Then in your main method 然后在你的主要方法
PasswordCheck password=new PasswordCheck(pwd);
password.setEightCharacters(); //call the method
password.setOneUppercase(); //call the method
password.setOneLowercase(); //call the method
password.setOneDigit(); //call the method
password.setPasswordValid();
My interpretation is that you need to have all your password verification methods as instance methods of PasswordCheck. 我的解释是,您需要拥有所有密码验证方法作为PasswordCheck的实例方法。
So something like 所以像
public PasswordCheck(){
private String password;
public PasswordCheck(String pwd){
password = pwd
}
// Below you have all your verification methods
public void isEightCharacters(){
if (password.length() >= 8){
System.out.println("Valid");
}
else {
System.out.println("invalid");
}
}
// etc.etc.
}
And in your main method do something like: 在您的主要方法中执行以下操作:
PasswordCheck password=new PasswordCheck(pwd);
// Call all your verification method
password.isEightCharacters();
// etc.etc.
我认为它想让您创建一个对象密码,并在构造函数中使其符合条件,密码很可能只有一个字符串变量可以接收,然后您可以检查该密码是否正确
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.