Send data to a method and check it one by one

DMT82 :

I got a challenge to make a Ulam Spiral in Java, but before the Ulam Spiral is created, the user should put in 2 numbers. Those 2 numbers should be checked so they aren't a prime number. As long as one of the numbers that are written is a prime number, then the program should restart and ask for 2 new numbers.

The getInput method is asking the user for 2 numbers. After that those numbers are written, the isPrime method should be called and one number at a time should be checked if it's a prime. If the first number checked isn't a prime, then the second number should be checked, if that also isn't a prime, then the next method should be called. What I don't understand is the isPrime method I have. It checks the number, but how do I make so if the first isn't a prime, then check the next number and as long as none of them are prime, then we are fine. I could make that the isPrime method takes in both numbers and they are checked, but I was not allowed to do that, I must check one number at a time.

Here you see my getInput and isPrime method. And yes, I know that isPrime(firstNumber); isPrime(secondsNumber); in my main method is weird and it shouldn't be like that, should it? is it a better way?

public class UlamSpiral {

    private static int firstNumber, secondNumber;
    private static Scanner scanner = new Scanner(System.in);
    private static boolean flag = false;

    public static void main(String[] args) {

        getInput();
        isPrime(firstNumber);
        isPrime(secondsNumber);
    }

    public static void getInput() {

        System.out.println("Enter two non-prime numbers");
        firstNumber = scanner.nextInt();
        secondNumber = scanner.nextInt();
    }

    public static boolean isPrime(int num) {

        for(int i = 2; i <= num / 2; ++i) {

            // Condition for non-prime number.
            if(num % i == 0) {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " Atleast one of the numbers are a prime number.");

        return true;
    }

EDIT: 1.
This is what I have now, but it won't work correctly.
When the program starts and I write in 2 prime numbers like 3, 5, then it tells me one of the numbers are prime and restart the program.
Seconds time, if I write in 3, 4, then the program continues even if 3 is a prime number.

public static void main(String[] args) {

    getInput();
    if(isPrime(firstNumber) || isPrime(secondNumber)){
        System.out.println("Atleast one of the numbers are a prime number.");
        getInput();
    } else {
        isMax(firstNumber, secondNumber);
    }

public static void getInput() {

    System.out.println("Enter two non-prime numbers");
    firstNumber = scanner.nextInt();
    secondNumber = scanner.nextInt();
}

public static boolean isPrime(int num) {
    boolean flag = true;

    for (int i = 2; i <= num / 2; ++i) {

        // Condition for non-prime number.
        if (num % i == 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

Output:

Enter two non-prime numbers 3 5 Atleast one of the numbers are a prime number. Enter two non-prime numbers 3 4

Process finished with exit code 0

Madhu Bhat :

Take the printing out of isPrime method and have it after both the numbers are checked. Refactor the code as below:

public class UlamSpiral {

    private static int firstNumber, secondNumber;
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        getInput();
        if(isPrime(firstNumber) || isPrime(secondNumber)){
            System.out.println("Atleast one of the numbers are a prime number.");
        } else {
           //scenario when none are prime
        }

    }

    public static void getInput() {

        System.out.println("Enter two non-prime numbers");
        firstNumber = scanner.nextInt();
        secondNumber = scanner.nextInt();
    }

    public static boolean isPrime(int num) {
        boolean flag = false;

        for(int i = 2; i <= num / 2; ++i) {

            // Condition for non-prime number.
            if(num % i == 0) {
                flag = true;
                break;
            }
        }

        return flag;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=151423&siteId=1
one
ONE