Results 1 to 2 of 2
Thread: Simple Password Check problem
- 12-26-2011, 03:51 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 8
- Rep Power
- 0
Simple Password Check problem
I'm trying to make a simple code that will check two imputs from the user.
My Code
OutputJava 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!");} } }
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
- 12-26-2011, 05:15 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
It is very similar to comparing a string with a string literal. And it is important to use equals() in both cases.How do I compare two string inputs from the user?
Java Code://if(password.equals("password1"){ //then say something} if(password1.equals(password2)) { // etc }
Similar Threads
-
Simple password validation.
By DarkAlex in forum New To JavaReplies: 27Last Post: 03-21-2011, 01:28 AM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum New To JavaReplies: 2Last Post: 11-14-2008, 07:53 PM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 0Last Post: 11-14-2008, 01:22 PM -
How to check password of a jsp/html with the password of Database(mysql) #1
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 2Last Post: 11-14-2008, 01:11 PM -
Problem with a password
By saytri in forum New To JavaReplies: 2Last Post: 12-27-2007, 11:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks