This what I have done so far, even though I check all of them I get AlertDialog message.
private void validateCheckBoxes() {
if (toilets.isSelected() || wifi.isSelected() || trolleys.isSelected() || lifts.isSelected()
&& ticketMachine.isSelected() || stepFree.isSelected()) {
saveRecordsToDatabase();
} else {
AlertDialog.Builder facilitiesError = new AlertDialog.Builder(AddStation.this);
facilitiesError.setTitle("Station Facilities are not selected");
facilitiesError.setMessage("Please select at least one facility ");
facilitiesError.setNegativeButton("OK", null);
facilitiesError.create().show();
}
}
By "selected" do you mean "checked"?
if (toilets.isChecked() || wifi.isChecked() || trolleys.isChecked() || lifts.isChecked()
|| ticketMachine.isChecked() || stepFree.isChecked()) {
saveRecordsToDatabase();
}
modify your code:
if (toilets.isSelected() || wifi.isSelected() || trolleys.isSelected() || lifts.isSelected()
|| ticketMachine.isSelected() || stepFree.isSelected()) {
saveRecordsToDatabase();
}
Explanation: you are using all && operators in if statement, which means you get alert message only if all the check boxes are checked.
If you use all || (or) operator, it means you get alert message if at least one checkbox is checked.
Related
Could you help me identify what I am missing with the code below? I am using Eclipse.
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
{
if (input.equalsIgnoreCase(("front door") || ("front") || ("basement") || ("basement entrance")))
if (input.equalsIgnoreCase(("front door") || ("front"))
System.out.println("Maggie went to the side of the home and open the basement door. As the door opened, she could smell the dust from inside.");
else
if ((input.equalsIgnoreCase("basement") || ("basement entrance")))
System.out.println("Maggie walks up the steps and slowly opens the front door.");
else
System.out.println("That is not a correct answer");
I think there are a few errors:
1. If-else logic:
a) if (input.equalsIgnoreCase(("front door") || ("front") ||
("basement") || ("basement entrance")))
if (input.equalsIgnoreCase(("front door") || ("front"))
What do you mean here? Second if is redundant.
b)
else
if ((input.equalsIgnoreCase("basement") || ("basement entrance")))
this part is unreachable, because if covers it.
Syntax error in if(condition).
Suppose it should look llke
String input = scanner.nextLine();
{
input = input.toLowerCase();
if (input.equals("front door") || input.equals("front"))
System.out.println("Maggie went to the side of the home and open the basement door. As the door opened, she could smell the dust from inside.");
else if (input.equals("basement") || input.equals("basement entrance"))
System.out.println("Maggie walks up the steps and slowly opens the front door.");
else
System.out.println("That is not a correct answer");
That's not how you use logical or operator, you have to do something like
if (input.equalsIgnoreCase("front door") || input.equalsIgnoreCase("front") ||
input.equalsIgnoreCase("basement") || input.equalsIgnoreCase("basement entrance")) {
...
}
I want to make a Java textfield for an email address, when I click submit it must contain an # and . in text for validation.
How do I do that?
My code:
private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {
if(txtName.getText().trim().equals(""))
{
JOptionPane.showMessageDialog(null, "Must have name");
jlblNameVer.setVisible(true);
}
//Phone Number Validation
if(txtPhoneNum.getText().length() < 10)
{
JOptionPane.showMessageDialog(null, "Must atleast 10 characters");
}
}
you are not checking null's, You might run into null pointer exception.. please take a look at code.
if(txtName==null || txtName.getText()== null ||txtName.getText().trim().length()==0)
{
//must have name message
}
//Phone Number Validation
if(txtPhoneNum==null || txtPhoneNum.getText()==null || txtPhoneNum.getText().length() < 10)
{
//Must atleast 10 characters
}
if(txtEmailAddr==null || txtEmailAddr.getText()== null ||
!(txtEmailAddr.getText().trim().contains("#") && txtEmailAddr.getText().trim().contains(".")))
{
//must have valid email address
}
if ((Problem != null && !notokcheckbox.isChecked()) || (Problem!=null && !ressolvedcheckbox.isChecked())) {
Toast.makeText(getActivity(), "Enable Ok Or Not/Ok", 100000).show();
} else {
Toast.makeText(getActivity(), "Sucess", 100000).show();
}
I am trying to apply validation I have two check Box notokcheckbox and ressolvedcheckbox.
if Problem != null and nither notokcheckbox, ressolvedcheckbox is not checked then it should display Enable Ok Or Not/Ok
or
if Problem != null or either notokcheckbox or ressolvedcheckbox is enable then it should Print Sucess.
while I am trying it with single check Box i mean its working fine but not with both.
Can u please tell me how to apply with two check Box:
if (Problem != null && !notokcheckbox.isChecked()) {
Toast.makeText(getActivity(), "Enable Ok Or Not/Ok", 100000).show();
} else {
Toast.makeText(getActivity(), "Sucess", 100000).show();
}
working fine.
please suggest me how to fix it.
If I understand correctly, you want to display the "Enable Ok Or Not/Ok" text when both are unchecked, and otherwise "Success" (when either or both are enabled). Try this:
if (Problem != null && (!notokcheckbox.isChecked() && !ressolvedcheckbox.isChecked())) {
Toast.makeText(getActivity(), "Enable Ok Or Not/Ok", 100000)
.show();
}
else {
Toast.makeText(getActivity(), "Sucess", 100000)
.show();
}
If you instead mean it should display the "Enable Ok Or Not/Ok" text when neither OR both are enabled, and "Success" if just one is enabled, try this:
if (Problem != null && !(notokcheckbox.isChecked() ^ ressolvedcheckbox.isChecked())) {
Toast.makeText(getActivity(), "Enable Ok Or Not/Ok", 100000)
.show();
}
else {
Toast.makeText(getActivity(), "Sucess", 100000)
.show();
}
Or better yet, use RadioButtons so only one option can be selected..
Simplifying:
if (problem != null && (!notOkCheckBox.isChecked() || !ressolvedCheckBox.isChecked()) {
...
} else {
...
}
The expression below is true if problem is null and one of the two (or both of them) checkboxes is checked.
p.s. pay attention to the case of your variables, you should respect java variable naming convention.
I have a snippet of code that consistently gives me an error:
do {
System.out.println("Choose Role: (Manager, Developer, QA) ");
role = scan.nextLine();
// For testing: ///////////////////////
System.out.println("role is: " + role);
////////////////////////////////////////////////
if (is_numeric(role)) {
System.out.println("Invalid Input.");
continue;
} else if (!role.equalsIgnoreCase("MANAGER") || !role.equalsIgnoreCase("DEVELOPER") || !role.equalsIgnoreCase("QA")) {
System.out.println("Invalid Role");
continue;
} else {
break;
}
} while (true);
I added the "For testing" block just to see, if for some reason there is something happening to the variable role, but its not. No matter how I write manager/developer/qa (whether in caps, small letters, etc) the "Invalid Role" is triggered and the loop goes over again.
Any suggestions?
Logically, this test is wrong
(!role.equalsIgnoreCase("MANAGER") ||
!role.equalsIgnoreCase("DEVELOPER") ||
!role.equalsIgnoreCase("QA"))
Why? Because if the role = "MANAGER", it does not equal "DEVELOPER" (or "QA") and vice-versa. I think you wanted
(!role.equalsIgnoreCase("MANAGER") &&
!role.equalsIgnoreCase("DEVELOPER") &&
!role.equalsIgnoreCase("QA"))
!role.equalsIgnoreCase("MANAGER") || !role.equalsIgnoreCase("DEVELOPER") ||
!role.equalsIgnoreCase("QA")
Is not what you want, replace || with &&.
In your code you're saying: If role is not "MANAGER" OR if role is not "DEVELOPER" OR if role is not "QA".
Due to Short-circuit evaluation, if the first condition is true, the others won't be evaluated because true || anything is always true.
Java if test short circuit for boolean expressions.
Change || to &&
Contrary to what everyone else is saying, if what you're trying to model is "not valid", then don't distribute the negative over each condition and use &&. It's not modelling the right thing. Model "valid", put parentheses around it and negate the whole thing:
if (!(role.equalsIgnoreCase("MANAGER") || role.equalsIgnoreCase("DEVELOPER") ||
role.equalsIgnoreCase("QA"))) {
//...
}
But this is a case where a helper method will improve code clarity greatly. First model what you want to model, not the inverse:
public boolean validRole(String role) {
return role.equalsIgnoreCase("MANAGER") ||
role.equalsIgnoreCase("DEVELOPER") ||
role.equalsIgnoreCase("QA");
}
Then your if statement is hard to get wrong and documents itself:
if (!validRole(role)) {
//...
}
After getting an action from the KeyListener, using the event.getKeyCode() and later on the KeyEvent.getKeyText(keyCode), how would i check if the outcome of .getKeyText(keyCode) is a single character like 'a' and not a whole word like "Space" ?
How about this:
KeyEvent.getKeyText(keyCode).length == 1
You can use getKeyChar() of the KeyEvent, and then'll you be certain that what you get back is a single char.
E.g. something like this:
public void keyTyped(KeyEvent e) {
keyChar = e.getKeyChar();
...
}
Try this:-
if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
{
if(KeyEvent.getKeyText(keyCode).length == 1)
{
//Only one character is pressed.
}
}
This solution worked for everything except the delete key and numpad keys...
event.getKeyChar() != '\uFFFF'
Because any Java outputs that character for non-renderable keys, it works quite consistently.
To fix the delete key problem...
event.getKeyChar() != '\uFFFF' && event.getKeyCode() != KeyEvent.VK_DELETE
It will return true if the key is printable, and false if not.