0

Can some one please help me figure out my errors in the Program.Java. there are two separate files which i separated with a comment. I am having trouble trying to get my methods to work. The point is for a user to input a password. if the old password is the same as changeit then old password can become newpassword. Please help!!

    //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); 


}
2
  • But there is no Program.Java. Commented Sep 22, 2015 at 16:55
  • That is not the proper syntax for a method Commented Sep 22, 2015 at 16:59

3 Answers 3

1

Your syntax for declaring methods is wrong. It should look like this:

//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); 
}

Except oldPassword is a String which doesn't have a setText method, so that won't work either.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Body of method always should be embedded between braces "{" and "}"

  2. Even for single if condition, always use braces "{" and "}"

    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); 
        }
    }
    

Comments

0

I made some changes to your code: looks Comments

//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

        }
 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.