This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I'm working on a project with a registration form. The form asks the user to
enter a password of their choice in a JPassword Field, and to enter it again in another JPassword Field.
I'm using a JOptionPane to prompt the user if the passwords do not match. but when i use passwordField.getPassword().toString() on the two, they don't match.
I have tried entering a basic "12345" for example on both but i still have no luck.
i know you should use .equals(), but what is the equivalent to this for "not equals" without using the "!=" operator.
here is the following code.
if(e.getSource() == submit)
{
String name = nameTextField.getText();
String address = addressTextField.getText();
String phoneNumber = numberTextField.getText();
String dob = datePicker.getDateFormatString();
String password = passwordField.getPassword().toString();
String password2 = passwordFieldTwo.getPassword().toString();
try
{
//If the user leaves the field empty
if(name == null || address == null || phoneNumber == null || dob == null
|| password == null || password2 == null)
{
//Error message appears to prompt the user to
//complete the form
JOptionPane.showMessageDialog(null, "All fields must be complete to submit.", "Woops", JOptionPane.ERROR_MESSAGE);
}
if(password != password2)
{
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
passwordField.setText(null);
passwordFieldTwo.setText(null);
}
Any help with this would be much appreciated.
The != related to equals() for String comparison is expressed by !x.equals(y)
As an example lets take your code, to see if the two passwords not match do the following:
if (!Arrays.equals(passwordField.getPassword(), passwordFieldTwo.getPassword())) {
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
}
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
It always give the second conditional regardless of the input. Is (userName == "Charles") the correct way of doing this conditional? or do i need something else.
import java.util.Scanner;
public class Name_input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userName = input.next();
System.out.println(userName);
if (userName == "Charles")
{
System.out.println("Correct Name");
}
else if (userName != "Charles")
{
System.out.println("Incorrect Name");
}
}
}
Is (userName == "Charles") the correct way of doing this conditional
No You don't use "==" to compare strings.
Instead you should be using the equals(...) method:
if ( "Charles".equals( userName ) )
Note I reversed the order of the comparison. This will prevent a NPE if the username is ever null.
System.out.println(userName);
Does that display the expected value? Do you need to use the trim() method?
else if (userName != "Charles")
Also, there is no need for the "else if". The name is either "Charles" or it isn't so you don't need the additional "if" statement.
can I ask how I can instantly equal my TextField named txtUserName into 'Aime' and also my txtPassword equal to 'Joy' so that it will show the message "User Name and Password Match!" ? Anyone? Please help me :(
public void actionPerformed(ActionEvent e){
if (e.getSource()== btnClear){
txtUserName.setText("");
}
if(e.getSource() == btnLogin){
if (txtUserName.setText="Aime" && txtPassword.setText="JOy")){
JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}
else {
JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}
These are the Small modifications needed.
if (txtUserName.getText().equals("Aime") && txtPassword.getText().equals("Joy")){
JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}else{
JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}
For your reference : What is the difference between == vs equals() in Java?
you have two problems here:
txtUserName.setText="Aime" && txtPassword.setText="JOy"
am guessing you are trying to check the user input so you need to use the gettext method...
on the other hand you can not compare strings using ==, but furthermore you mistyped the == and are instead of comparing assigning, assignment that is invalid since you can not set the text of that view in that way...
try instead
txtUserName.getText().toString().equals("Aime") && ...
This question already has answers here:
JOptionPane - check user input and prevent from closing until conditions are met
(2 answers)
Closed 6 years ago.
I want a user to input 4 things (first name, last name, email and phone number) and in case he misses one or more just one JOptionPane window would show.
String firstname = TF_1.getText();
String lastname = TF_2.getText();
String email = TF_3.getText();
String phonenumber = TF_4.getText();
if (TF_1.equals("") ||
TF_2.equals("") ||
TF_3.equals("") ||
TF_4.equals("")) {
JOptionPane.showMessageDialog(null, "All text fields must be filled");
First thing personally I don't like one generic message for all fields it puts users in confusion what they have not filled. You can use a flow that you check all field one by one and if a field is empty show error for it and return from their.
Secondly check on string fields firstname, lastname etc not on TF_1.
public boolean isEmpty(String... fields)
{
for (String string : fields)
{
if (null == string || string.trim().length() == 0)
{
// return true of filed is blank
return true;
}
}
return false;
}
You can use this method and pass all your fields to check if any of them is empty.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm still newbie to java programming. And can anybody tell me what's wrong with the source code? When I run the code, the conditional assignment always outputs "login failed".
import java.util.Scanner;
public class ProgramBiodataMahasiswa {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String username, password, output;
System.out.print("Enter username : ");
username = input.nextLine();
System.out.print("Enter password : ");
password = input.nextLine();
output = (username=="kesit" && password=="ps123") ? "login successfully" : "login failed" ;
System.out.println(output);
}
}
Use .equals
output = (username.equals("kesit") && password.equals("ps123")) ? "login successfully" : "login failed" ;
With Strings ("quest" and "ps123") you shouldn't be using == to check if they equal. This will compare the pointer and due to the fact that String in Java are immutable, the pointers will always be different. Therefore use
username.equals("kesit") && password.equals("ps123").
That should work!
You can't compare strings in Java using ==. You should use equals method. e.g. username.equals("kesit") && password.equals("ps123")
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i have a username and a password string. How can I compare them in Android? This is somewhat what I'm looking for:
if( user== username && pass == password){ then do this }
else if(user[does not equal to]username && pass==password{ toast (invaild username.)}
else if(user==username && pass[does not equal] password{toast(invaild password)}
else{ toast [invaild login]}
I know that's not correct coding but the two "else if" statements is what I'm looking for. The rest is just to give a better understanding of what I'm trying to do.
The username is what's entered into a Edittext same with the Password so they are both String when i .getText.toString right?
To compare the text in strings use equals() e.g. pass.equals(password) rather than == which checks if two strings are the same object.
For "does not equal" you can negate the statement e.g. !pass.equals(password).
Use the equals method for comparing strings.
if( user.equals(username) && pass.equals(password))
{
then do this
}
else if(!user.equals(username) && pass.equals(password)
{
Toast.makeText(this,"invalid username",Toast.LENGTH_SHORT).show();
}
else if(user.equals(username) && !pass.equals(password))
{
Toast.makeText(this,"invalid password",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this,"Invalid login",Toast.LENGTH_SHORT).show();
}
You want to use .equals(..) to compare Strings. Also, to do not logically, you can use !.
//define user, username
//define pass, password
if(user.equals(username) && pass.equals(password)){
//then do this
}
else if(!user.equals(username) && pass.equals(password)){
//toast (invaild username.)
Toast.makeText(getApplicationContext(), "invalid username", Toast.LENGTH_SHORT).show();
}
else if(user.equals(username) && !pass.equals(password)){
//toast(invaild password)
Toast.makeText(getApplicationContext(), "invalid password", Toast.LENGTH_SHORT).show();
}
else{
//toast [invaild login]
Toast.makeText(getApplicationContext(), "invalid login", Toast.LENGTH_SHORT).show();
}
Here are some sources on Toast:
http://www.mkyong.com/android/android-toast-example/
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
You could use pass.equal(password).