Dre Jay Dre Jay - 2 months ago 6
Java Question

Prime number method not working

I am trying to create a program that allows the user to input a chosen amount of numbers, and in return the user gets two lists. One shows which of the numbers are prime numbers, the other gives non-primes. Why doesn't this work? I end up getting all the numbers in the same array, when I want them in separate arrays. When initializing isPrime to true in the isPrime method, all the numbers go into the prime array and into the notPrime array when I initialize it as false.

public static void main(String[] args) {
System.out.println("How many numbers do you want to input?");
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int[] values = new int[number];
for(int i = 0; i < values.length; i++) {
System.out.println("Give value number " + (i+1) + ":");
values[i] = in.nextInt();
}
int[] prime = new int[values.length];
int[] notPrime = new int[values.length];
for(int i = 0; i < values.length; i++) {
boolean erPrimtall = erPrimtall(values[i]);
if(isPrime == true) {
prime[i] = values[i];
} else {
notPrime[i] = values[i];
}
}
System.out.println("Prime numbers:");
for(int i = 0; i < values.length; i++){
System.out.println(prime[i]);
}
System.out.println("Other numbers:");
for(int i = 0; i < values.length; i++){
System.out.println(notPrime[i]);
}
}
inn.close();
}

static boolean isPrimtall(int values) {
boolean isPrime = true;
for(int i = 2; i < Math.sqrt(values); i++) {
if(values % i == 0) {
isPrime= false;
break;
}
}
return isPrime;
}

Answer

Your erPrimtall function has true/false values reversed.

static boolean erPrimtall(int values) {
    boolean isPrime = true;
    for(int i = 2; i < values; i++) {
        if(values % i == 0) {
            isPrime= false;
            break;
        }
    }
    return isPrime;
}

You could optimize this function further by looping only until i <= Math.sqrt(values)

I also suggest using an ArrayList instead of arrays to store the prime and non-primes otherwise you will have to do some bookkeeping to keep track of how many primes/non-primes there are so far.

Full revised code:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        System.out.println("How many numbers do you want to input?");
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        int[] values = new int[number];
        for(int i = 0; i < values.length; i++) {
            System.out.println("Give value number " + (i+1) + ":");
            values[i] = in.nextInt();
        }
        List<Integer> prime = new ArrayList<Integer>();
        List<Integer> notPrime = new ArrayList<Integer>();
        for(int i = 0; i < values.length; i++) {
            if(erPrimtall(values[i])) {
                prime.add(values[i]);
            } else {
                notPrime.add(values[i]);
            }
        }
        System.out.println("Prime numbers:");
        for(int n : prime){
            System.out.println(n);
        }
        System.out.println("Other numbers:");
        for(int n : notPrime) {
            System.out.println(n);
        }
        in.close();
    }

    static boolean erPrimtall(int values) {
        boolean isPrime = true;
        for(int i = 2; i <= Math.sqrt(values); i++) {
            if(values % i == 0) {
                isPrime = false;
                break;
            }
        }
        return isPrime;
    }
}