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).
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm trying to check the string if a sentence has a word "TIN" but it says false when i try to use equal or '=='. Here is my code.
if (!recognizer.isOperational()) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
} else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = recognizer.detect(frame);
StringBuilder sb = new StringBuilder();
//get text from sb until there is no text
for (int it = 0; it < items.size(); it++) {
TextBlock myItem = items.valueAt(it);
sb.append(myItem.getValue());
sb.append("\n");
if (myItem.getValue() == ("TIN")) {
Toast.makeText(this, "VERIFIED ID", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "WRONG ID", Toast.LENGTH_SHORT).show();
}
}
//set text to edit text
mResultEt.setText(sb.toString());
}
When writing myItem.getValue()==("TIN") you only check if the handles of the two objects are identical. To compare their contents, you must write myItem.getValue().equals("TIN").
I think you want to check if it contains the value "TIN" in string.
If you want to check value contain in a string then use below -
if (myItem.getValue().contains ("TIN")) {
//... code
}
if a sentence has a word "TIN" means you want to check a particular word in a sentence.
So use, myItem.getValue().toString().contains("TIN")
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:
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);
}
I have 3 edittexts with input type as number and 1 button that say Done
I have to check two conditions for the values entered in the edittexts
When I click on the button Done I should be able to do thee following things
----First is to check if the edit texts are empty or not
if ((a.getText().toString().equals("")) || (b.getText().toString().equals("")) || (c.getText().toString().equals("")))
Toast.makeText(getApplicationContext(), "Don't leave grade points empty",0).show();
----Second is to check if the values entered are within a certain range
if((a1<5 || a1>10)||(b1<5 || b1>10)||(c1<5 || c1>10))
Toast.makeText(getApplicationContext(), "Range should be between 15 to 25", 0).show();
both a,b,c and a1,b1,c1 represent the same values, i've just parsed the values of a,b,c to int in a1,b1,c1
----Then in the else part I should be able to use these values if they pass the above conditions.
else
{
//do something
}
My problem is how to make it work as I face a Force close error on clicking the Done button when all 3 edittext are empty. I tried to put those in if, else if and else loop and also tried if,if and else loop.
How should I code that if it doesn't satisfy both the conditions and then go to the else part?
Help !! :)
if ((a.getText().toString().equals("")) || (b.getText().toString().equals("")) || (c.getText().toString().equals("")))
Toast.makeText(getApplicationContext(), "Don't leave grade points empty",0).show();
else {
//Convert your a/b/c.getText() to a1/b1/c1 now you know they are not empty
if((a1<5 || a1>10)||(b1<5 || b1>10)||(c1<5 || c1>10))
Toast.makeText(getApplicationContext(), "Range should be between 15 to 25", 0).show();
else
{
//do something
}
}
I think it is because you are trying to perform some kind of check on a variable that is null. You could try:
if (a.getText() == null || b.getText() == null || c.getText() == null ){
//Toast code
} else {
//Convert string to int code
if ((a1<5 || a1>10) || (b1<5 || b1>10) || (c1<5 || c1>10)){
//Toast code
} else {
//Do something with numbers in the correct range code here
}
}
It would also help to look at your logcat output. You should see a line that says: "Caused by ...." which will tell you what is causing your force close.
I think you just need to make the second if an else if
if ((a.getText().toString().equals("")) || (b.getText().toString().equals("")) || (c.getText().toString().equals("")))
Toast.makeText(getApplicationContext(), "Don't leave grade points empty",0).show();
else if((a1<5 || a1>10)||(b1<5 || b1>10)||(c1<5 || c1>10))
Toast.makeText(getApplicationContext(), "Range should be between 15 to 25", 0).show();
else {
//logic
}
Kind of an odd flow of execution, but I think this will fix your problem
Not an Android programmer but, if x.getText() returns null, .toString() produces NullPointerException.
create method:
private static boolean isEmpty(Button button)
{
if (button != null)
{
final String text = button.getText();
return text != null && text.length() > 0;
}
return false;
}
then:
if (isEmpty(a) || isEmpty(b) || isEmpty(c)
......
Do like this, will definitely work, CHECKED:
if ((a.getText().toString().equals("")) || (b.getText().toString().equals("")) || (c.getText().toString().equals("")))
Toast.makeText(getApplicationContext(), "Don't leave grade points empty",0).show();
else{
if((a1<5 || a1>10)||(b1<5 || b1>10)||(c1<5 || c1>10))
Toast.makeText(getApplicationContext(), "Range should be between 15 to 25", 0).show();
else
{
//do something
}}