JCheckBox already checked but it says else statement - java

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.

Related

Multiple conditions in if statement - first is always true

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))

Exception in thread "main" java.util.NoSuchElementException in if statem [duplicate]

This question already has answers here:
Scanner NoSuchElementException when calling .next() method
(3 answers)
Closed 5 years ago.
if (charIte.next()=='{' || charIte.next()=='}'
|| charIte.next()=='[' || charIte.next()==']'
|| charIte.next()=='(' || charIte.next()==')'
|| charIte.next()=='*' || charIte.next()=='"'
|| charIte.next()=='/'){
}
The program returns:
Exception in thread "main" java.util.NoSuchElementException at line
|| charIte.next()=='(' || charIte.next()==')'
What is the problem?
Each invocation of next() consumes one token. Call it once, and save and then compare with the result. Like,
char ch = charIte.next();
if (ch == '{' || ch == '}' || ch == '[' || ch == ']' || ch == '('
|| ch == ')' || ch == '*' || ch == '"' || ch == '/') {
// ...
}
Each time you do charIte.next() you're asking to read the next token.
I think what you should do to ischar ite = charIte.next.chartAt(0) and then use ite in your if statement
if (ite next()=='{' || next()=='}'
|| ite.next()=='[' || ite.next()==']'
|| ite.next()=='(' || ite.next()==')'
|| ite.next()=='*' || ite.next()=='"'
|| ite.next()=='/'){
Info on Scanner

Only last line is being written into a file [duplicate]

This question already has answers here:
PrintWriter append method not appending
(5 answers)
How to append text to an existing file in Java?
(31 answers)
Closed 5 years ago.
I need HELP. I tried to separate the valid serial keys from invalid ones. I got the output correctly. but then when i tried to write it in a file, ONLY the last line is being written.
the output is:
1A000000
1A000001
1A000002
1A000003
1A000004
1A000005
2B200012
3C343455
4D342423
5E324344
6F435435
7G245347
and I want to write this to a file. But ONLY 7G245347 is being written.
import java.util.*;`
import java.io.*;
public class ValidSerialKey {
public static void main(String[] args) throws IOException {
String keys = "";
File file = new File("serialkeys.txt");
try{
Scanner scan = new Scanner(file);
while (scan.hasNext()){
keys = scan.nextLine();
if ((keys.charAt(0) == '1' || keys.charAt(0) == '2' || keys.charAt(0) == '3' || keys.charAt(0) == '4' || keys.charAt(0) == '5' ||
keys.charAt(0) == '6' || keys.charAt(0) == '7' || keys.charAt(0) == '8' || keys.charAt(0) == '9' ) &&
(keys.charAt(1) == 'A' || keys.charAt(1) == 'B' || keys.charAt(1) == 'C' || keys.charAt(1) == 'D' || keys.charAt(1) == 'E' ||
keys.charAt(1) == 'F' || keys.charAt(1) == 'G' || keys.charAt(1) == 'H' || keys.charAt(1) == 'I' || keys.charAt(1) == 'J' ||
keys.charAt(1) == 'K' || keys.charAt(1) == 'L' || keys.charAt(1) == 'M' || keys.charAt(1) == 'N' || keys.charAt(1) == 'O' ||
keys.charAt(1) == 'P' || keys.charAt(1) == 'Q' || keys.charAt(1) == 'R' || keys.charAt(1) == 'S' || keys.charAt(1) == 'T' ||
keys.charAt(1) == 'U' || keys.charAt(1) == 'V' || keys.charAt(1) == 'W' || keys.charAt(1) == 'X' || keys.charAt(1) == 'Y' ||
keys.charAt(1) == 'Z' )){
System.out.println(keys);
File filein = new File("ValidKeys.txt");
try{
try
(PrintWriter pw = new PrintWriter(filein)){
pw.print(keys);
pw.close();
}
}catch (FileNotFoundException ex){
System.out.println(ex.getMessage());
}
}//end of if
}//end of while
scan.close();
}catch (FileNotFoundException exp){
System.out.println(exp.getMessage());
}
}
}
You want to keep the PrintWriter opened to write other things during the next iterations of the loop
So don't create a new one at each iteration.
As a side note, you don't need to close explicitly the PrintWriter instance when you use try with resources.
You should replace this logic :
loop
try
(PrintWriter pw = new PrintWriter(uniqueFile)){
pw.print(keys);
}//end of inner try
end loop
by a logic where you include the whole logic in the try with resources statement :
try(PrintWriter pw = new PrintWriter(uniqueFile)){
loop
pw.print(keys);
end loop
}
catch (IOException e){
... // exception handling
}

Row edit iterator in Apache Accumulo

I'm working with Apache Accumulo and I need to create a iterator that every minor compaction scans all whole row and create a MD5 of two column and save it as another column of my row...
Example:
I insert this data:
|| Row || colFam || colQual || value ||
||=====||========||=========||=======||
|| A || person || name || Bob ||
|| A || person || surname || Smith ||
|| A || work || place || Bank ||
|| B || person || name || Jhon ||
|| B || person || surname || Allen ||
|| B || work || place || Pub ||
...
...
I need a iterator that every time I write a row (A or B with all its colFam e colQual) it get the value of two column (name and surname) and calculate the MD5 of the resulting string (name + surname) and save it as a column of my row.
The result should be like that:
|| Row || colFam || colQual || value ||
||=====||========||=========||==============||
|| A || person || name || Bob ||
|| A || person || surname || Smith ||
|| A || work || place || Bank ||
|| A || MD5 || MD5 || <MD5 result> || <--
|| B || person || name || Jhon ||
|| B || person || surname || Allen ||
|| B || work || place || Pub ||
|| B || MD5 || MD5 || <MD5 result> || <--
....
....
I think that I can put this iterator on a minor(/major) compaction of a table.
Any idea? Which one of the Built-In iterator I have to extend to do that?
Thank you so much
Check out the TransformingIterator. This iterator tries to hide some of the complexities behind row-level operations.
The general strategy is that, when iterating over a row, you have to buffer the row in memory, perform your computation, and then write out the row in the correct sorted order.

Loop has gone crazy

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.

Categories

Resources