Why is the first if statement always true?
private String setDepartment (){
int code = Integer.parseInt(JOptionPane.showInputDialog("Enter The Department Code:\n" +
"1:Sales\n" +
"2:Development\n" +
"3:Accounting\n" +
"4:None"));
/*Why this if statement is always true? How do i solve it? */
if (code !=1 || code !=2 || code !=3 || code !=4)
{
JOptionPane.showMessageDialog(null, "Invalid Number.Enter a number between 1-4");
setDepartment();
}
if (code==1){
return "Sales";
}
else if (code==2){
return "Development";
}
else if (code==3){
return "Accounting";
}
else
return "";
}
Replace || with &&:
if (code !=1 && code !=2 && code !=3 && code !=4)
You need to use AND instead of OR.
If the user enters 1 then it's automatically different than 2,3 and 4.
You can therefore use :
if (code !=1 && code !=2 && code !=3 && code !=4)
OR
if (code ==1 || code ==2 || code ==3 || code ==4)
From first principles, picking code = 1:
code !=1 || code !=2 || code !=3 || code !=4
= 1 !=1 || 1 !=2 || 1 !=3 || 1 !=4
= false || true || true || true
= true
You probably meant && rather than ||.
Because at any point of time code value would be any of 1-4. And 3 of 4 conditions would always be true. Change your if condition to
If( !( code ==1 || code == 2 || code == 3 || code == 4))
Related
Rules for valid Indian mobile number:
The number should contain 10 or 11 or 12 digits.
If it contains 10 digits, then the first digit should be 7 or 8 or 9.
If it contains 11 digits, then the first digit should be 0 and the second rule followed.
If it contains 12 digits, then the first two digits should be 91 and the second rule followed.
For test case:
1
881906355596
this code should produce Invalid but it is showing Valid.
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int t=scan.nextInt();
while((t--)!=0){
String s = scan.next();
int length = s.length();
if((length==10) &&((s.charAt(0)=='7')||(s.charAt(0)=='9')||(s.charAt(0)=='8')))
System.out.println("Valid");
else if((length==11) &&(s.charAt(0)=='0')&&(s.charAt(0)=='7')||(s.charAt(0)=='9')||(s.charAt(0)=='8'))
System.out.println("Valid");//code
else if((length==12) &&(s.charAt(0)=='9')&&(s.charAt(1)=='1'))
System.out.println("Valid");//code
else System.out.println("Invalid");
}
}
}
Your second and third conditions are wrong.
The second condition incorrectly returns true for your 881906355596 input.
You'll see why if you arrange it as follows:
else if (
(length==11) && // false &&
(s.charAt(0)=='0') && // false &&
(s.charAt(0)=='7') || // false ||
(s.charAt(0)=='9') || // false ||
(s.charAt(0)=='8') // true
) // equals true
It should be:
else if (length == 11 && s.charAt(0) == '0' && (s.charAt(1) == '7' || s.charAt(1) == '9' || s.charAt(1) == '8'))
The third condition should be:
else if (length == 12 && s.charAt(0) == '9' && s.charAt(1) == '1' && (s.charAt(2) == '7' || s.charAt(2) == '9' || s.charAt(2) == '8'))
You missed one whole ()
else if((length==12) &&((s.charAt(0)=='9')&&(s.charAt(1)=='1')))
I create Hospital Management System for School projects and I think i am at 35% of the system and here at Patient Registration where I struggle a lot here is the `JFrame1 I have at First Page
First Page
St First Page I have this Past Medical History and Review Symptoms with other options so if I don't check any of the JCheckBox controls including the None check box but I click the next Button it will pop up the option if you don't have any Past Medical History the codes like this
if(!jCheckBox1.isSelected() || !jCheckBox2.isSelected() || !jCheckBox3.isSelected() || !jCheckBox4.isSelected() || !jCheckBox5.isSelected() || !jCheckBox6.isSelected() || !jCheckBox7.isSelected() || !jCheckBox8.isSelected() || !jCheckBox9.isSelected() || !jCheckBox10.isSelected() || !jCheckBox11.isSelected() || !jCheckBox12.isSelected()|| !jCheckBox13.isSelected() || !jCheckBox14.isSelected() || !jCheckBox15.isSelected() || !jCheckBox16.isSelected() || !jCheckBox17.isSelected() ||
!jCheckBox18.isSelected() || !jCheckBox19.isSelected() || !jCheckBox20.isSelected() || !jCheckBox21.isSelected() || !jCheckBox104.isSelected() || !jCheckBox102.isSelected() || !jCheckBox100.isSelected() || !jCheckBox98.isSelected() || !jCheckBox111.isSelected() || !jCheckBox94.isSelected() || !jCheckBox95.isSelected() || !jCheckBox99.isSelected() || !jCheckBox109.isSelected() || !jCheckBox112.isSelected() || !jCheckBox110.isSelected() || !jCheckBox97.isSelected() ||
!jCheckBox107.isSelected() || !jCheckBox101.isSelected() || !jCheckBox106.isSelected() || !jCheckBox96.isSelected() || !jCheckBox105.isSelected() || !jCheckBox92.isSelected() || !jCheckBox103.isSelected()|| !jCheckBox93.isSelected() || !jCheckBox105.isSelected() || !jCheckBox115.isSelected() || !jCheckBox114.isSelected() || !jCheckBox113.isSelected() || !jCheckBox121.isSelected() || !jCheckBox120.isSelected() || !jCheckBox119.isSelected() || !jCheckBox118.isSelected() ||
!jCheckBox117.isSelected()|| !jCheckBox116.isSelected() || !jCheckBox127.isSelected()){
//JOptionPane.showMessageDialog(null, "Medical History is empty","Error",JOptionPane.WARNING_MESSAGE);
int yesOrno = JOptionPane.showConfirmDialog(null, "No medical History?", "Proceed",JOptionPane.YES_NO_OPTION);
if(yesOrno == 0){
jCheckBox127.setSelected(true);
PARENTPANEL.removeAll();
PARENTPANEL.add(ALLERGIES);
PARENTPANEL.repaint();
PARENTPANEL.revalidate();
jCheckBox1.setEnabled(false);
jCheckBox1.setSelected(false);
jCheckBox2.setEnabled(false);
jCheckBox2.setSelected(false);
jCheckBox3.setEnabled(false);
jCheckBox3.setSelected(false);
JOptionPane Popup even when I already select one
I already checked the check boxes and click next but pop up always showed up.
changing || into && in the if clause may save you.
The problem is that, the first condition in the while loop does not get executed at all even if its true. If i remove the Logical OR from the while loop and just write the first condition (selection.compareToIgnoreCase("O") >0) it works fine. But if there are two conditions with Logical OR, it does not work.
I've tried using equals(), I've also tried to negate the logic using
while(!selection.equals("O") || !selection.equals("E")). The second condition works fine but the first does not work at all.
public class OddsOrEvens {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Let’s play a game called \"Odds and Evens\"");
System.out.println("Whats your name ? ");
String name = sc.nextLine();
System.out.println("Hi "+ name +", which do you choose? (O)dds or (E)vens?");
String selection = sc.nextLine();
System.out.println("selection: " + selection);
while (selection.compareToIgnoreCase("O") >0 || selection.compareToIgnoreCase("E") >0){
System.out.println("Please enter the correct choice. Select 'O' for odds or 'E' for evens");
selection = sc.next();
}
if(selection.equalsIgnoreCase("O")){
System.out.println(name + " has picked Odds! The computer will be evens.");
}else if (selection.equalsIgnoreCase("E")){
System.out.println(name + " has picked Evens! The computer will be Odds.");
}
}
}
Your string comparison is not correct. Compareto returns -1/0/1 for less/equal/greater.
A clearer way to do this is to use toUppercase().equals(....
while (!selection.toUpperCase().equals("O") && !selection.toUpperCase().equals("E")){
That is for not to hold for two cases, one needs !... && ! ... An OR || would have the effect of being always true, as at least one of the cases is false. Alternatively !(... || ...).
while (!selection.equalsIgnoreCase("O") && !selection.equalsIgnoreCase("E")) {
Let's simplify:
!(n == 1) || !(n == 2) // WRONG
n != 1 || n != 2 // WRONG
will always be true, as either n == 1 is false or n == 2 is false: at most one choice can be true, falsifying the others. So on at least on side is !false, true, so the entire expression is true.
!(n == 1) && !(n == 2) // GOOD
n != 1 && n != 2 // GOOD
The mental mistake is that the linguistic OR mostly means EXCLUSIVE OR.
Possible would have been the equivalent:
!(n == 1 || n == 2) <=> n != 1 && n != 2 [De Morgan's law]
I am trying to write a program that forms a random 2 character word and then asks the user to guess the word. After the user inputs a guess, the program should check the guess with the answer. The program should then give the user a hint depending on their guess in the form nAkB where nA is the right letters in the right position and kB is the right letters but in the wrong position. My problem is that my program won't show the hint when their are right letters but in the wrong position (say the answer is aD and the user guesses Dc) the output should be "Hint: 0A1B". Here is my code for the getHint Method:
public static String getHint(String guess, String answer){
String hint = "No";
if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) == answer.charAt(1)){
hint = "You win!";
}
else if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 1A0B";
}
else if(guess.charAt(0) != answer.charAt(0) && guess.charAt(1) == answer.charAt(1)){
hint = "Hint: 1A0B";
}
else if(guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(1) == answer.indexOf(guess.charAt(1))){
hint = "Hint: 0A2B";
}
else if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) == answer.indexOf(guess.charAt(1)) &&
guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 1A1B";
}
else if(guess.charAt(1) == answer.charAt(1) && guess.charAt(0) == answer.indexOf(guess.charAt(0)) &&
guess.charAt(0) != answer.charAt(0)){
hint = "Hint: 1A1B";
}
else if(guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(0) != answer.charAt(0) &&
guess.charAt(1) != answer.indexOf(guess.charAt(1)) && guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 0A1B";
}
else if(guess.charAt(1) == answer.indexOf(guess.charAt(1)) && guess.charAt(1) != answer.charAt(1) &&
guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(0) != answer.charAt(0)){
hint = "Hint: 0A1B";
}
else{
hint = "Hint: 0A0B";
}
return hint;
It never outputs anything with a 1B or 2B. It only does 1A0B or 0A0B.
Your issue is code like guess.charAt(0) == answer.indexOf(guess.charAt(0)) . indexOf will return the index of the character. So you are comparing the index of a character (rhs) to the ASCII value of the character (lhs) and your if statements using this indexOf construction are not being entered.
Rather, consider something like this:
else if(answer.indexOf(guess.charAt(0))!=-1 && answer.indexOf(guess.charAt(1)) != -1{
hint = "Hint: 0A2B";
}
From Java API, indexOf returns -1 if the given character is not found within the string.
This loop keeps giving me my error message even when I enter a valid input and I can't find what is wrong with it. It's the same as my other loops in the program which all work fine. Does anyone know the problem? Here is the loop:
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
while((gender != 'm') || (gender != 'f')) {
System.out.println("ERROR Please enter a valid age");
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
}//end while
Now if I type 'm' or 'f' it will give me the error message? ??? ???
while((gender != 'm') || (gender != 'f')) {
should really be
while((gender != 'm') && (gender != 'f')) {
The condition (gender != 'm') || (gender != 'f') always evaluates to true. To understand why, consider one case - gender = 'm'...
(gender != 'm') || (gender != 'f')
('m' != 'm') || ('m' != 'f')
false || true
true
Basically, java doesn't know the common English idiom if gender isn't (either) male or female.