Simple Password Check problem
I'm trying to make a simple code that will check two imputs from the user.
My Code
Code:
import java.util.Scanner;
public class Practice1 {
public static void main(String[] args){
Scanner scIn = new Scanner(System.in);
System.out.print("Please enter your password!");
String password1 = scIn.nextLine();
System.out.print("Please reenter your password!");
String password2 = scIn.nextLine();
if(password1==password2){
System.out.print("Welcome!");}
else{
System.out.print("Please reenter your password!");}
}
}
Output
Please enter your password!
G
Please reenter your password!
G
Please reenter your password!
As You can see I entered the same password twice (G). However, instead of the output being "welcome!" it says "Please reenter your password!"
Im pretty sure if I wanted to compare 1 string my code would look like
if(password.equals("password1"){
then say something}
How do I compare two string inputs from the user?
Re: Simple Password Check problem
Hi Gsangha, Welcome to the Forums!
When you post code use the "code" tags. You put [code] at the start of the code and [/code] at the end. This means that the code will keep its formatting and be more readable.
Quote:
How do I compare two string inputs from the user?
It is very similar to comparing a string with a string literal. And it is important to use equals() in both cases.
Code:
//if(password.equals("password1"){
//then say something}
if(password1.equals(password2)) {
// etc
}