registration system error with input - java

Unfortunately, I do not understand the problem with this part of my code.
I'm setting up a registration system.
It seems to be wrong when recording correct information.
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty() &&
!(name.length() < 4) && !(password.length()<5) && email.lastIndexOf(".") - email.indexOf("#") > 2 &&
email.contains(".") && email.lastIndexOf("#") < email.lastIndexOf(".")
&& email.contains("#") ) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Something is Wrong! ", Toast.LENGTH_LONG)
.show();
}
please, help me to edit this. thank you

Use this to validate emails:
public final static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches()); }
It is possible that your email validation is wrong here.
Your final code snippet:
if (!name.isEmpty() && !name.length() < 4) && (!password.isEmpty() &&!(password.length()<5)) && isValidEmail(emailId)) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Something is Wrong! ", Toast.LENGTH_LONG)
.show();
}

instead of messing up your validation do a neat check. Either use this email validation
public static boolean isValidEmaillId(String email){
return Pattern.compile("^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))#"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$").matcher(email).matches();
}
Or this one:
public static boolean isValidEmail(CharSequence target) {
return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
and then do your validation like this:
if (!name.isEmpty() && !name.length() < 4) && (!password.isEmpty() &&!(password.length()<5)) && isValidEmail(emailId)) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Something is Wrong! ", Toast.LENGTH_LONG)
.show();
}

Related

If return null, failed test Tennis Kata Java

I found Tennis Kata training for TDD, but I have small problem with If statement.
public String getScore(){
if (server.getScore() >= 3 && receiver.getScore() >= 3){
if(Math.abs(receiver.getScore() - server.getScore()) >= 2){
return getLeadPlayer().getName() + " won";
} else if (Math.abs(server.getScore() - receiver.getScore()) >= 1) {
return "A" + ":" + receiver.getScoreDescription();
} else if (Math.abs(receiver.getScore() - server.getScore()) >= 1) {
return server.getScoreDescription() + ":" + "A";
} else {
return "40:40";
}
} else {
return server.getScoreDescription() + ":" + receiver.getScoreDescription();
}
The second else if statement return null. I have no idea what is wrong.
#Test
public void receiverAdvantage() {
IntStream.rangeClosed(1, 3).forEach((Integer) -> {
server.winBall();
});
IntStream.rangeClosed(1, 4).forEach((Integer) -> {
receiver.winBall();
});
assertThat(tennisGame, hasProperty("score", is("40:A")));
}
"A" mean advantage.
And the Test result:
java.lang.AssertionError:
Expected: hasProperty("score", is "40:A")
but: null

If else condition and logical operators

This is my code for searching a 2 places that call the method of the polyline. When my editext Tabaco - Malinao it's calling the method properly same as Tabaco-Bacacay, Tabaco-Santo Domingo, Tabaco-Malilipot. But when my edittext is Malilipot-Santo Domingo, Santo Domingo-Bacacay it's always calling for the first condition (Tabaco-Malinao) not their own condition.
Could be the condition is wrong? or the logical operators that I'm using?
My understanding is that, it takes what comes first and not taking the equal sign correctly. This could be wrong.
String origin = etOrigin.getText().toString();
String destination = etDestination.getText().toString();
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco")) {
ttom();
Toast.makeText(getApplicationContext(), "tabaco malinao", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Tabaco")) {
ttob();
Toast.makeText(getApplicationContext(), "tabaco bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Santo Domingo") && origin.equals("Santo Domingo") == destination.equals("Tabaco")) {
ttosto();
Toast.makeText(getApplicationContext(), "tabaco sto domingo", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Tabaco") == destination.equals("Malilipot") && origin.equals("Malilipot") == destination.equals("Tabaco")) {
ttomali();
Toast.makeText(getApplicationContext(), "tabaco malilipot", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Malilipot") == destination.equals("Santo Domingo") && origin.equals("Santo Domingo") == destination.equals("Malilipot")) {
malitosto();
Toast.makeText(getApplicationContext(), "malilipot sto domingo", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Malilipot") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Malilipot")) {
malitobac();
Toast.makeText(getApplicationContext(), "malilipot bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Santo Domingo") == destination.equals("Bacacay") && origin.equals("Bacacay") == destination.equals("Santo Domingo")) {
bactosto();
Toast.makeText(getApplicationContext(), "sto domingo bacacay", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
}
The logic is wrong, let's say that you have :
String origin = "Malilipot";
String destination = "Santo Domingo";
Then origin.equals("Tabaco") is false
and destination.equals("Malinao") is also false .
So origin.equals("Tabaco") == destination.equals("Malinao") yields false == false which is true, despite the fact that it absolutely doesn't match what you thought was your condition.
Refactor your code to only use && (AND) and || (OR) operators :
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
Becomes
if ((origin.equals("Tabaco") && destination.equals("Malinao")) || (origin.equals("Malinao") && destination.equals("Tabaco")))
Change the condition like below ,in your case when you enter Malilipot-Santo Domingo as per first condition
if (origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
origin.equals("Tabaco") is false and destination.equals("Malinao") is also false then false == false it become true same for next condition
String origin = etOrigin.getText().toString();
String destination = etDestination.getText().toString();
if ((origin.equals("Tabaco") && destination.equals("Malinao")) ||( origin.equals("Malinao") == destination.equals("Tabaco"))) {
ttom();
Toast.makeText(getApplicationContext(), "tabaco malinao", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Tabaco") && destination.equals("Bacacay")) || ( origin.equals("Bacacay") && destination.equals("Tabaco"))) {
ttob();
Toast.makeText(getApplicationContext(), "tabaco bacacay", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Tabaco") && destination.equals("Santo Domingo")) ||( origin.equals("Santo Domingo") && destination.equals("Tabaco")) ){
ttosto();
Toast.makeText(getApplicationContext(), "tabaco sto domingo", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Tabaco") && destination.equals("Malilipot") )||( origin.equals("Malilipot") && destination.equals("Tabaco"))) {
ttomali();
Toast.makeText(getApplicationContext(), "tabaco malilipot", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Malilipot") && destination.equals("Santo Domingo") )||( origin.equals("Santo Domingo") && destination.equals("Malilipot"))){
malitosto();
Toast.makeText(getApplicationContext(), "malilipot sto domingo", Toast.LENGTH_SHORT).show();
} else if ((origin.equals("Malilipot") && destination.equals("Bacacay")) ||( origin.equals("Bacacay") && destination.equals("Malilipot"))) {
malitobac();
Toast.makeText(getApplicationContext(), "malilipot bacacay", Toast.LENGTH_SHORT).show();
} else if (origin.equals("Santo Domingo") && destination.equals("Bacacay") )||( origin.equals("Bacacay") && destination.equals("Santo Domingo"))) {
bactosto();
Toast.makeText(getApplicationContext(), "sto domingo bacacay", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Invalid input!", Toast.LENGTH_SHORT).show();
}
here is the explanation why it's going in the first condition...
String origin = "Malilipot";
String destination = "Santo Domingo";
if(origin.equals("Tabaco") == destination.equals("Malinao") && origin.equals("Malinao") == destination.equals("Tabaco"))
origin.equals("Tabaco") = false
destination.equals("Malinao") = false
origin.equals("Tabaco") == destination.equals("Malinao")
false == false
result will be = true
origin.equals("Malinao") = false
destination.equals("Tabaco") = false
origin.equals("Malinao") == destination.equals("Tabaco")
false == false
result will be = true
at your last condition will be if (true == true)
it will be : true
thats why its going in first condition

Validating Multiple edittext and showing edittext.setError("String");

I am trying to validate some edittext. I Made 7 edittexts. Phone number ,First name , Last name , Pickup Address, Postal Code, Pickup time, Number of products. As u can see i made 7 edittexts and I want to validate them so that when i press the button to go to next page it does all the database posting and getting on that button. I've already made a method (public void addInfo) and now i am adding the validations on the View.OnClick implemented methods. Here is my Code i wrote for validations.
public void onClick(View v) {
if (v == nextbuttonjava) {
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
}
if(firstnamejava.getText().toString().length() == 0)
{
firstnamejava.setError("Whats your first name?");
}
if(lastnamejava.getText().toString().length() == 0)
{
lastnamejava.setError("Whats your last name?");
}
if(pickupaddressjava.getText().toString().length() == 0)
{
pickupaddressjava.setError("Enter your address");
}
if(postalcodejava.getText().toString().length() < 6)
{
postalcodejava.setError("Invalid postal code");
}
if(pickuptimejava.getText().toString().length() == 0)
{
pickuptimejava.setError("Enter pickuptime");
}
if(numberstuffjava.getText().toString().length() == 0) {
numberstuffjava.setError("Enter number of clothes");
}
else
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
}
}
I Want that every If statement should be true to make the else work so the intent works and i go to the other activity. But its not the scene here. Only the phone number works good(If i enter the phone number below 10 digits it shows the error and it doesn't run else part). My main concern is that I Have 7 edittexts and Even if one If statement goes true then the else shouldn't be running and i shouldn't go to next intent with posting incomplete data on my database.I Want that even if any of the edittexts if statement goes wrong i should get an error (eg, numberstuffjava.setError("Input Something");.
How to resolve the issue?
Try something like this:
public void onClick(View v) {
if (v == nextbuttonjava) {
boolean error = false;
if(phnumberjava.getText().toString().length() < 10)
{
error = true;
phnumberjava.setError("Invalid Phone Number");
}
if(firstnamejava.getText().toString().isEmpty())
{
error = true;
firstnamejava.setError("Whats your first name?");
}
if(lastnamejava.getText().toString().isEmpty())
{
error = true;
lastnamejava.setError("Whats your last name?");
}
if(pickupaddressjava.getText().toString().isEmpty())
{
error = true;
pickupaddressjava.setError("Enter your address");
}
if(postalcodejava.getText().toString().length() < 6)
{
error = true;
postalcodejava.setError("Invalid postal code");
}
if(pickuptimejava.getText().toString().isEmpty())
{
error = true;
pickuptimejava.setError("Enter pickuptime");
}
if(numberstuffjava.getText().toString().isEmpty())
{
error = true;
numberstuffjava.setError("Enter number of clothes");
}
if(!error)
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
} else {
//manage error case here
}
}
}
another option could be this:
if (v == nextbuttonjava) {
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
}
else if(firstnamejava.getText().toString().isEmpty())
{
firstnamejava.setError("Whats your first name?");
}
else if(lastnamejava.getText().toString().isEmpty())
{
lastnamejava.setError("Whats your last name?");
}
else if(pickupaddressjava.getText().toString().isEmpty())
{
pickupaddressjava.setError("Enter your address");
}
else if(postalcodejava.getText().toString().length() < 6)
{
postalcodejava.setError("Invalid postal code");
}
else if(pickuptimejava.getText().toString().isEmpty())
{
pickuptimejava.setError("Enter pickuptime");
}
else if(numberstuffjava.getText().toString().isEmpty())
{
numberstuffjava.setError("Enter number of clothes");
}
else
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
else
{
//manage error case here
}
}
In this way you check any case with the else if. I prefer the first solution because it's more "flexible".
Hope this helps!
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
return "Error message";
}
else {
return null;
}
return like this in each and every if condition. If the return statement is null, then all if conditions are true. else show the error message returned
Among other things, you can try the following logic. Here, you can be assured that all the error messages will be shown:
if (v == nextbuttonjava) {
boolean validated = true;
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
validated = false;
}
if(firstnamejava.getText().toString().length() == 0)
{
firstnamejava.setError("Whats your first name?");
validated = false;
}
if(lastnamejava.getText().toString().length() == 0)
{
lastnamejava.setError("Whats your last name?");
validated = false;
}
if(pickupaddressjava.getText().toString().length() == 0)
{
pickupaddressjava.setError("Enter your address");
validated = false;
}
if(postalcodejava.getText().toString().length() < 6)
{
postalcodejava.setError("Invalid postal code");
validated = false;
}
if(pickuptimejava.getText().toString().length() == 0)
{
pickuptimejava.setError("Enter pickuptime");
validated = false;
}
if(numberstuffjava.getText().toString().length() == 0) {
numberstuffjava.setError("Enter number of clothes");
validated = false;
}
// fire in the hole !
if (validated)
{
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
}
The else condition it's only attached to the numberstuffjava if condition, in this scenario the else condition will be called always when this if condition is false.
Try changing the previous if conditions with else if.
You just create a method which return true only if all your needs are satisfied. if the method return true, then only go to next activity. I will give an example here.
public boolean isValid(){
if(phnumberjava.getText().toString().length() < 10)
{
phnumberjava.setError("Invalid Phone Number");
return false;
}
if(firstnamejava.getText().toString().length() == 0)
{
firstnamejava.setError("Whats your first name?");
return false;
}
return true;
}
Then do things like below.
if(isValid()){
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}
just add else if for checking all validation
if (v == nextbuttonjava) {
if (edittext1.getText().toString().length() < 5) {
edittext1.setError("error");
} else if (edittext2.getText().toString().length() == 0) {
edittext2.setError("error");
} else {
// your intent here
}
}
add else if for every of your edittext .
Try out below method to check an empty value of edittext.
public static boolean m_isError = false;
/**
* This is method to check edit text empty or not.
*
* #param p_editText
* - edittext
* #param p_nullMsg
* -display message if edittext null
*/
public static void validateForEmptyValue(EditText p_editText,
String p_nullMsg) {
if (p_editText != null && p_nullMsg != null) {
// use trim() while checking for blank values
if (TextUtils.isEmpty(p_editText.getText().toString().trim())) {
m_isError = true;
p_editText.setError(p_nullMsg);
}
}
}
Use the above method as below for each of your edittext:
CustomValidator.m_isError = false;
CustomValidator.validateForEmptyValue(metfirstname, "enter firstname", metfirstname);
CustomValidator.validateForEmptyValue(metlastname, "enter lastname", metlastname);
if (!CustomValidator.m_isError) {
//Define your logic once all the validations are done.
addInfo();
Intent nextpageintent = new Intent(Afterfullscreen.this, Extrainformation.class);
startActivity(nextpageintent);
}

JFrame submit button confirm

I have a submit button in my program/JFrame it checks for validations and brings up error messages and then another form pops up and has all entered details:
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
//VALIDATIONS-----------------------------------------------------------
if(txtName.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
}
else
{
jlblNameVer.setVisible(false);
}
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
}
//EMAIL VALIDATION
if(txtEmail==null ||txtEmail.getText().length() < 10|| txtEmail.getText()== null ||!(txtEmail.getText().trim().contains("#") && txtEmail.getText().trim().contains(".")))
{
JOptionPane.showMessageDialog(null, "Invalid Email");
}
//Phone Number Validation
if(txtPhoneNum.getText().length() < 10)
{
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
}
//COMBOBOX VALIDATIONS
if(cmbStayDuration.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(null, "Please select stay duration");
}
//Photo ID
if(cmbPhotoId.getSelectedIndex() == -1)
{
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
}
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
The problem is even when these validations are wrong the form will pop up anyway
If any of the validations are incorrect I don't want the popup form to show, what must I do?
You either need to restructure you if blocks into a if-else if-else block, so that last (else) condition would show the new view or use a boolean field which determines if the validation passed or not, for example...
boolean passed = true;
if (txtName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
passed = false;
} else {
jlblNameVer.setVisible(false);
}
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
passed = false;
}
//EMAIL VALIDATION
if (txtEmail == null || txtEmail.getText().length() < 10 || txtEmail.getText() == null || !(txtEmail.getText().trim().contains("#") && txtEmail.getText().trim().contains("."))) {
JOptionPane.showMessageDialog(null, "Invalid Email");
passed = false;
}
//Phone Number Validation
if (txtPhoneNum.getText().length() < 10) {
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
passed = false;
}
//COMBOBOX VALIDATIONS
if (cmbStayDuration.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select stay duration");
passed = false;
}
//Photo ID
if (cmbPhotoId.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
passed = false;
}
if (passed) {
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
or
if (txtName.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
} else {
jlblNameVer.setVisible(false);
//ID VERIFICATION
if (txtIdNumber.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "Photo Id must not be emplty");
passed = false;
} else if (txtEmail == null || txtEmail.getText().length() < 10 || txtEmail.getText() == null || !(txtEmail.getText().trim().contains("#") && txtEmail.getText().trim().contains("."))) {
JOptionPane.showMessageDialog(null, "Invalid Email");
passed = false;
} else if (txtPhoneNum.getText().length() < 10) {
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
passed = false;
} else if (cmbStayDuration.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select stay duration");
passed = false;
} else if (cmbPhotoId.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "Please select Photo ID type");
passed = false;
} else {
//Popup form
jfrmDetails nf1 = new jfrmDetails();
jfrmDetails.txtRoomTypef2.setText(this.cmbRoomType.getSelectedItem().toString());
jfrmDetails.txtRoomNumf2.setText(this.cmbRoomNumber.getSelectedItem().toString());
jfrmDetails.txtCheckIn.setText(this.ftxtCheckinDate.getText());
jfrmDetails.txtCheckOut.setText(this.txtCheckOut.getText());
jfrmDetails.txtName.setText(this.txtName.getText());
jfrmDetails.txtIdType.setText(this.cmbPhotoId.getSelectedItem().toString());
jfrmDetails.txtIdNum.setText(this.txtIdNumber.getText());
jfrmDetails.txtPhoneNum.setText(this.txtPhoneNum.getText());
jfrmDetails.txtEmail.setText(this.txtEmail.getText());
nf1.setVisible(true);
}
}
You should also have a look at The Use of Multiple JFrames, Good/Bad Practice? and maybe consider using a CardLayout instead, but this will depend on your underlying needs
You could also have a look at Validating Input, which would allow you to do post validation of your fields when the lose focus

Android EditText validation

I have two EditText like editText1 and editText2.
I am taking input from EditText to name1 and name2.
Now I want to validate that the EditText are not empty before clicking on ok button.
For this I used:
if(editText1==NULL && editText2==NULL) {
textView.setText("please enter Names");
} else {
other code;
}
But its not validating, and with empty EditText it is showing result on clicking ok button.
I have also tried in this way:
if(name1==NULL && name2==NULL) {
textView.setText("please enter Names");
} else {
other code;
}
What to do?
Try this:
if(editText1.getText().toString().trim().equals("")
&& editText2.getText().toString().trim().equals(""))
{
textView.setText("Please enter Names");
}
else
{
// other code
}
Please use:
if(name1.compareTo("") == 0 || name2.compareTo("") == 0) {
// Your piece of code for example
Toast toast=Toast.makeText(getApplicationContext(), "ENTER NAMES", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
} else {
// Your code
}
This will work definitely for sure.
Try like this name1.equals("") in place of name1==NULL
if(name1.equals("") && name2.equals(""))
{
textView.setText("please enter Names");
}
else
{
other code;
}
if(name1.compareTo("") == 0 || name2.compareTo("") == 0)
{
// Your piece of code for example
}
you can use:
if(name.compareTo("") == 0 || name.compareTo(null) == 0) {}
This works very well for me, try this
if(editText1.getText().toString().length == 0 && editText2.getText().toString().length == 0)
{
textView.setText("Please enter names");
}
else
{
//some other actions to perform
}
if(editText1.getText().toString().length() > 0 && editText2.getText().toString().length() > 0)
{
textView.setText("Please enter Names");
}
else
{
// Other code
}

Categories