0

The basis of my problem is here: https://github.com/experiencethebridge1/primeGap

Bottom line, I want to create an array in which the output of a method will populate the elements of the new array.

This is not homework.

package primenumbermethod;

import java.util.Scanner;

public class PrimeNumberMethod {

    public static void main(String[] args) {
        System.out.print("How many prime numbers do you want to work with? ");
        Scanner input = new Scanner(System.in);
        int arraySize = input.nextInt();

        // Invoke printPrimeNumbers method
        System.out.println("If I can ever get it to work, the number of the "
                + "elements in the array I want to build will be " + arraySize +".");

        System.out.println();
        printPrimeNumbers(arraySize);

        // How can I read parts of a method into elements of an array?
        int[] myList = new int[arraySize];


    }

    public static int printPrimeNumbers(int numberOfPrimes) {
        final int NUMBER_OF_PRIMES_PER_LINE = 10;  // Display 10 per line
        Scanner input = new Scanner(System.in);
        System.out.print("What number do you want to start from?  ");
        int number = input.nextInt();
        int count = 0; // Count the number of prime numbers
        // Repeatedly find prime numbers
        while (count < numberOfPrimes) {
            // Print the prime number and increase the count
            if (isPrime(number)) {
                count++; // Increase the count
                if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
                    // Print the number and advance to the new line
                    System.out.printf("%-15d\n", number);
                } else {
                    System.out.printf("%-15d", number);
                }
            }
            number++;
        }
        return 0;
    }
    // Method for checking if number is prime
    public static boolean isPrime(int number) {
        for (int divisor = 2; divisor <= number / 2; divisor++) {
            if (number % divisor == 0) {// If true, number is not prime 
                return false;  // Number is not a prime    
            }
        }
        return true; // Number is prime
    }
}

Tried using global variables, abstraction does not apply (but could).

The main method initiates the program, then traces to method printPrimeNumbers, then into method boolean isPrime. I want to return the output of that method into a new array...

The array size will be defined by the user input <"How many prime numbers do you want to work with? ">, and then <"What number do you want to start with?>

Problem, I can't seem to pass the output of a method into the elements of an array.

Thoughts?

3
  • You can pass a reference to the the array to the method. The method can write into the array whose reference was passed. For example, you could declare a method like this - myMethod( int[] array ) and call it like this: myMethod( myList ). Commented Feb 18, 2019 at 15:37
  • Thank you, that is very helpful. I've read that before too, don't know why I forgot. I appreciate the perspective. Commented Feb 18, 2019 at 15:48
  • I advice against changing the array that is passed in, it is bad practice to mutate the parameters. It is very non-obvious behaviour for the caller. Commented Feb 19, 2019 at 8:33

1 Answer 1

1

I would suggest you should restructure your code in the following way:

public static void main(String[] args) {
    int numberOfPrimes = readIntFromCommandLine...;
    int numberToStartWith = readIntFromCommandLine...;

    int[] primeNumbers = getPrimeNumbers(numberOfPrimes, numberToStartWith);

    // maybe extract this to another method as well
    for (int prime : primeNumbers) {
        // do whatever you want with prime, e.g. print it - or sum it, or multiply or whatever
    }
}

public static int[] getPrimeNumbers(int amount, int from) {
    int[] primes = new int[amount];
    int count = 0;

    /* now put your current prime logic here and whenever you 
       find a prime set primes[count] = newlyFoundPrime;  */
}

public static boolean isPrime(int number) { /* stays the same */ }

It is generally a good idea to only ask for user input at a well defined point in your code, not all over the place. Therefore I placed the two inputs at the front. Another generally good idea is to make every method (maybe except for the main method) only do one thing. Your isPrime is a good example of that. Moving the printing logic out of getPrimeNumbers simplifies that method and lets you handle the printing at another, dedicated place.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll give that a shot. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.