I have this code right now which prints whether an integer is positive/negative, odd/even, prime/composite. I just want the value of kctr to be printed to the print function after I have called kCheck method. The for loop is correct but after calling kCheck it just prints 0.
import javax.swing.JOptionPane;
public class FirstClass
{
public int kCheck(int num, int k, int kctr)
{
for(int l=1;l<num+1;l++){
if(( num%l) == 0){
kctr++;
//System.out.println("" +kctr); Just to check if the loop is corrrect
}
}
return kctr;
}
public static void main (String args[])
{
CheckClass checker = new CheckClass();
FirstClass checker2 = new FirstClass();
int num = 0;
int even = 0;
int odd = 0;
int negeven = 0;
int negodd = 0;
int k = 0;
int kctr = 0;
num = Integer.parseInt(JOptionPane.showInputDialog (null, "Input an integer.", JOptionPane.QUESTION_MESSAGE));
boolean ch=true;
while(ch){
if(num%2 == 0 && num>0){
ch=false;
even = 1;
}
else if(num%2 == 1 && num>0){
checker2.kCheck(num, k, kctr);
odd = 1;
System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here.
It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}
else if(num%2 == 0 && num < 0){
negeven = 1;
}
else if(num%2 == 1 && num < 0){
negodd = 1;
}
break;
}
if(even == 1){
checker.posEvenCheck(num);
}
if(odd == 1){
checker.posOddCheck(num, kctr);
}
if(negeven == 1){
checker.negEvenCheck(num);
}
if(negodd == 1){
checker.negOddCheck(num);
}
}
}
Change this part of the code:
else if(num%2 == 1 && num>0){
checker2.kCheck(num, k, kctr);
odd = 1;
System.out.println("" +kctr); /*Just to check if the loop is correct and the problem is here.
It prints the value of kctr on the method kCheck but when it comes to here, it prints 0.*/
}
To:
else if(num%2 == 1 && num>0){
kctr = checker2.kCheck(num, k, kctr); // <- Assign the return value of the method to kctr
odd = 1;
System.out.println("" +kctr);
}