Results 1 to 2 of 2
  1. #1
    Gsangha is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Simple Password Check problem

    I'm trying to make a simple code that will check two imputs from the user.


    My Code
    Java 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?
    Last edited by pbrockway2; 12-26-2011 at 05:12 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    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.

    Java Code:
    //if(password.equals("password1"){
    //then say something}
    if(password1.equals(password2)) {
        // etc
    }

Similar Threads

  1. Simple password validation.
    By DarkAlex in forum New To Java
    Replies: 27
    Last Post: 03-21-2011, 01:28 AM
  2. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  4. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM
  5. Problem with a password
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •