Hi, I was asked to create a Java programthat takes 2 words as input from the key
ID: 3783412 • Letter: H
Question
Hi, I was asked to create a Java programthat takes 2 words as input from the keyboard, that should represent a password adn the same password again. (Just like prompting a user to enter a password and entering it again when creating an account on a website.) Basically, if both passwords match, then it should notify the user it has a registered account, otherwise they will be notified of a typo in the password they try to create. Below is a program I have working in progress. Can you look at it and show the modifications with comments? Thank you.
import java.util.Scanner; //Needed for the Scanner class
public class YourLastNameValidPassword
{
public static void main(String[] args)
{
//to hold the user's input
String password;
//to hold the user's input
String open = null;
//A Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Prompt the user to enter the password.
System.out.println("Please enter your password: ");
boolean password1 = keyboard.nextLine();
//Determine if the user entered the right password.
if (password1 == open)
{
System.out.print("Please re-enter your password: ");
password1 = keyboard.nextLine();
if(password1 = open != null)
{
System.out.println("Sorry, there is a typo in your password.");
}
else
{
System.out.println("You are now registered as a new user.");
}
}
else
{
System.out.println("Sorry, there is a typo in your password.");
}
}
}
Explanation / Answer
Yup,there are mistakes in the program.I have listed below the mistakes and also re-written the code.I have made the modified codes as block so it would be helpful for you to point out.
Hope,I had helped you.Any further queries please feel free to ask us.We will love to help you
Mistakes:
1) boolen password1=keyboard.nextLine(); this is wrong- one can't convert string into boolean
2)password1 == open ; this is give false even if password1="open" because == operator refers to objects ;it checks whether both the element refer to same object or not.But,it don't the content
Instead use Objects.equal();
Modified Code:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.