使用Java方法创建密码检查器

时间:2015-09-22 16:52:43

标签: java class methods

有人可以帮我解决我在Program.Java中的错误。有两个单独的文件,我用评论分开。我无法尝试让我的方法发挥作用。关键是用户输入密码。如果旧密码与changeit相同则旧密码可以成为新密码。请帮忙!!

    //my Main.Java File
package Main;
import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
        //initialize the scanner, old password and new password//

        Scanner passwordScanner = new Scanner(System.in);
        String oldPassword;
        String newPassword;

        //initializing password object//

        Password passwordObject = new Password();

        //ask user for old password and new password//

        System.out.println("Please enter your old password: ");
        oldPassword = passwordScanner.nextLine();

        System.out.println("Please enter your new password: ");
        newPassword = passwordScanner.nextLine();

        //Call the methods//

        passwordObject.checkPassword(oldPassword);
        passwordObject.changePassword(oldPassword, newPassword);

    }

}


//-----------------------------------------------------------------------//
//my Password.java file
package Main;

public class Password {
    //intialize password to change it//
    String password = "changeit";
    String somePassword;
    boolean succefulChange;

    //Check password method//

    public boolean checkPassword(String somePassword);

    //return true is some password is same as old password, if not then return false//
    if (somePassword.equals(oldPassword))

        return true;
    else

        return false; 


    //Change password method//
    public void changePassword(String oldPassword, String newPassword);



    //Change old password to new password if old password is true//
    if (oldPassword.equals(password))

        oldPassword.setText(newPassword); 


}

3 个答案:

答案 0 :(得分:1)

声明方法的语法错误。它应该是这样的:

//Change password method//
public void changePassword(String oldPassword, String newPassword) {
  //Change old password to new password if old password is true//
  if (oldPassword.equals(password))
      oldPassword.setText(newPassword); 
}

除了oldPassword是一个没有setText方法的字符串,所以它也不会工作。

答案 1 :(得分:0)

  1. 方法体总是应该嵌入大括号之间" {"和"}"

  2. 即使是单身if条件,也要使用大括号" {"和"}"

    public boolean checkPassword(String somePassword){
    
        if (somePassword.equals(oldPassword)){
            return true;
        }
        else{
            return false; 
        }
    }
    
    
    public void changePassword(String oldPassword, String newPassword){
    
        if (oldPassword.equals(password)){
            oldPassword.setText(newPassword); 
        }
    }
    

答案 2 :(得分:0)

我对您的代码进行了一些更改:看起来评论

//my Main.Java File
package Main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner passwordScanner = new Scanner(System.in);
    String oldPassword;
    String newPassword;

    Password passwordObject = new Password();

    System.out.println("Please enter your old password: ");
    oldPassword = passwordScanner.nextLine();

    System.out.println("Please enter your new password: ");
    newPassword = passwordScanner.nextLine();


    passwordObject.checkPassword(newPassword,oldPassword);
    newPassword = passwordObject.changePassword(oldPassword, newPassword); // here old password can become newpassword

}

}

  //-----------------------------------------------------------------------   //
//my Password.java file
package Main;

public class Password {

     String password = "changeit";
     String somePassword;
     boolean succefulChange;

public boolean checkPassword(String somePassword,String oldPassword){  // take two password for check and begin method with "{" not ";"


if (somePassword.equals(oldPassword))

    return true;
else

    return false; 
}


public String changePassword(String oldPassword, String newPassword){ //begin method with "{" not ";" and change void method to String to return the new password 




if (oldPassword.equals(password)){

                                      //for testing:  System.out.println("old password is the same as changeit he can become new password");
     return newPassword;  //String doesn't have a setText method so return the newPassword if password="changeit"
 }
else{
                                   //for testing: System.out.println("old password is not the same as changeit he can not become new password");
    return oldPassword; //if password is not equal to "changeit" it can not be changed so return the old one

        }
 }

}