AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: How to write code for reset password in core Java?

Write core java code to reset the password which will ask you below fields:
old password
new password
confirm password

password successfully updated

corejava x 353
programming x 169
Posted On : 2018-08-03 18:41:30.0
profile Rajesh Sadhiya - anyforum.in Rajesh Sadhiya
200
up-rate
2
down-rate

Answers


I´ld explain it in simple manner using properties file. But practically you must go with some database and Java jdbc. Instead of using database, I used properties file for a reference which is user.properties. In which key refers user name and value refers password for respective user.

user.properties:
---------------------------------------
User123=Password123
User456=Password456
User789=Password789


ResetPassword.java:
------------------------------------------------------
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Scanner;

public class ResetPassword {
private static Scanner input=new Scanner(System.in);
private static String filePath="D:/in/anyforum/user.properties";
public static void main(String[] args) {
System.out.println("Please Enter your user name:");
String userName= input.nextLine();
System.out.println("Please Enter your old password:");
String password= input.nextLine();
boolean isLogin = login(userName, password);
if(isLogin){
System.out.println("Enter new password:");
String newPassword = input.nextLine();
System.out.println("Confirm password:");
String confirmPassword = input.nextLine();
if(newPassword.equals(confirmPassword)){
//use JDBC instead
Properties properties=getProperties();
properties.setProperty(userName, newPassword);
saveProperties(properties);
System.out.println("Password Reset Successfully!");
}else{
System.out.println("New password and confirm password doesn´t match");
}
}else{
System.out.println("Sorry! Invalid Username Password.");
}
}

public static boolean login(String userName, String password){
boolean isLogin=false;
//use JDBC instead
Properties properties=getProperties();
String actualPassword=properties.getProperty(userName);
if(password.equals(actualPassword))
isLogin=true;

return isLogin;
}

private static Properties getProperties(){
Properties properties = null;
try{
properties=new Properties();
FileInputStream fis= new FileInputStream(filePath);
properties.load(fis);
fis.close();
}catch (Exception e) {
e.printStackTrace();
}
return properties;
}

private static void saveProperties(Properties properties){
try{
FileOutputStream fos= new FileOutputStream(filePath);
properties.store(fos, null);
}catch (Exception e) {
e.printStackTrace();
}
}

}

Posted On : 2018-08-04 13:46:32
Satisfied : 4 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188247605
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in