How to accept user input until the input is even


#1

I thought I had done it correctly, but the output is never correct. Any help would be appreciated.

Here is my code:

public class Journal4B 
{
 Scanner in = new Scanner(System.in);
 public int getEvenValue()
 {
     System.out.println("Enter a number ");
 int val;
 val = in.nextInt();
     while( val %2==0;) false 
             {
     System.out.println("Enter a number ");
 val = in.nextInt();
 
 }
 return val;
 }
   
    public static void main(String[] args) 
    {
       Journal4B c = new Journal4B();
       int x =c.getEvenValue();
       
      
    }

#2

You posted in the wrong forum by the way. But try this

Edit:

public class Journal4B
{
	Scanner in = new Scanner(System.in);
	public int getEvenValue(){
		System.out.println("Enter a number ");
		int val = in.nextInt();
		while( val %2!=0){
			System.out.println("Enter a number ");
			val = in.nextInt();
		}
		return val;
	}
    public static void main(String[] args) 
    {
       Journal4B c = new Journal4B();
       int x =c.getEvenValue();
       System.out.println("You entered : "+ x);
 
    }

or here you can do it all in the main like this

import java.util.*;

public class Journal4B {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int user_input = in.nextInt();
		System.out.println("Enter a number ");
		while(user_input%2 != 0){
			System.out.println("Enter a number ");
			user_input = in.nextInt();
		}
		System.out.println("You entered: "+user_input);
	}
}