javahelp javahelp - 5 months ago 9
Java Question

java program with methods and boolean

import java.util.Scanner ;
public class ProcessNumbers
{
public static void main( String[] args )
{
Scanner in = new Scanner(System.in) ;
System.out.print("Please enter an integer between 6 and 12, inclusive: ") ;
int num = in.nextInt() ;
boolean result = shouldProcess(num);
String result1 = String.valueOf(result) ;
}
public static boolean shouldProcess(int n)
{
if (n>=6 && n<12)
{
return true;
}
else
{
return false;
}
}
public static boolean processInput(boolean result2)
{
if (result2 == true)
{
System.out.println("Yes") ;
}
else
{
System.out.println("No") ;
}
return result2 ;
}


}


now I am getting the output which is partially right but has forgot the yes or no output in the second method

Please enter an integer between 6 and 12, inclusive:


when it should also include the yes or not output

Answer

You are sending in a boolean value in the method parameter of processInput but you are catching it as a String. You need to change it to boolean. Further, you want to check if its value is true with equal signs like below:

public static void processInput(boolean result2)
 {
     if (result2 == true)
     {
         System.out.println("Yes") ; 
     }
     else 
     {
         System.out.println("No") ;
     }
 }

EDIT 2: Also, you need to change String result1 = String.valueOf(result); to processInput(result);

EDIT 3: If you want the number printed too that you just entered and then you want a "yes" or "no", then between int num = in.nextInt(); and boolean result = shouldProcess(num);, add this line: System.out.println(num);

Comments