0

This program is meant to display an array and compute prime numbers between 1 and whatever the user enters. On some IDEs that "Capture Output", the list of prime numbers will not "word-wrap". Instead, it will display one VERY long line of numbers. This can be handled by inserting a "line-feed" in the display code that is activated every 15 numbers. I have no clue how to do this, my code is below.

import java.util.Scanner;
import java.text.DecimalFormat;

public class Lab11avst {
    public static void main(String[] args) {
        // This main method needs additions for the 100 point version.
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the primes upper bound ====>>   ");
        final int MAX = input.nextInt();
        boolean primes[];
        primes = new boolean[MAX];
        computePrimes(primes);
        displayPrimes(primes);
    }

    public static void computePrimes(boolean primes[]) {
        System.out.println("\nCOMPUTING PRIME NUMBERS");
        int newLine = 15;
        int multiplicator = 1;
        int list[] = new int[1000];
        for (int k=2; k < primes.length; k++) {
            primes[k] = true;
        }
        for (int k=2; k < primes.length; k++)
            for (int x=2*k;x<primes.length;x+=k)
                primes[x] = false;
    }

    public static void displayPrimes(boolean primes[]) {
        DecimalFormat output = new DecimalFormat("0000");

        System.out.println("\n\nPRIMES BETWEEN 1 AND " + primes.length);
        int numPrimes = 0;
        for (int k=2; k < primes.length; k++) {
            if (numPrimes % 15 == 0) System.out.println("");
            if (primes[k]) System.out.print(output.format(k) + " ");
            ++numPrimes;
        }
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • windows uses ```\r\n``` as line terminator. use ```System.out.println``` to be platform independant, or ```System.getProperty("line.separator")``` – spi Dec 20 '17 at 14:38
  • Don't use backslash escapes. The literal causing a new line is returned by the system independent `java.lang.lineSeparator()`. – laune Dec 20 '17 at 14:43
  • Setup a counter in your loop and use the % operator. – Romain Hippeau Dec 20 '17 at 14:45
  • It's better to put those square brackets behind the data type, instead of behind the variable name. It's [preferred by](https://stackoverflow.com/questions/129178/difference-between-int-array-and-int-array) [many people](https://softwareengineering.stackexchange.com/questions/331358/when-declaring-an-array-in-java-what-is-the-conventional-location-for-the-squar), plus [conventions discourage that form](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – MC Emperor Dec 20 '17 at 14:53

2 Answers2

0

If you have to proceed with your current method of computing prime numbers, then you can just keep track of how many primes have been printed, and add a line break for every 15 numbers. I suggest making a slight change to your displayPrimes() method:

public static void displayPrimes(boolean primes[]) {
    DecimalFormat output = new DecimalFormat("0000");

    System.out.println("\n\nPRIMES BETWEEN 1 AND " + primes.length);
    int numPrimes = 0;
    for (int k = 2; k < primes.length; k++) {
        if (numPrimes % 15 == 0) System.out.println("");
        if (primes[k]) System.out.print(output.format(k) + " ");
        ++numPrimes;
    }
}

I use System.out.println here, which guarantees that the correct line break will be used for any platform.

However, a nicer way to approach the entire problem would be to just compute the actual prime numbers themselves, and then just iterate that array and display. Using this approach would require a complete refactor of code, maybe not what you want, and also probably too broad for a single question.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Let numPrimes indeed count the printed primes:

int numPrimes = 0;
for (int k = 2; k < primes.length; k++) {
    if (primes[k]) {
        if (numPrimes % 15 == 0) {
            System.out.println();
        }
        System.out.print(output.format(k) + " ");
        ++numPrimes;
    }
}

And \n is on Unix/Linux and MacOS, Windows uses \r\n:

    System.out.println("\nCOMPUTING PRIME NUMBERS");

should be

    System.out.println();
    System.out.println("COMPUTING PRIME NUMBERS");
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138