0

The problem is to display the first 50 prime numbers in 5 lines, each of which contains 10 numbers. I've created a program to output the first 50 prime numbers but I don't know how to split them so they output 10 numbers per line. I am a beginner level programmer and I really need help on this.

public class Lab4 {
    public static void main(String[] args) {
        int i = 0;
        int num = 0;

        String primeNumbers = " ";

        for (i = 1; i <= 230; i++) 
        {
            int counter = 0;

            for (num = i; num >= 1; num--) 
            {
                if (i % num == 0) 
                {
                    counter = counter + 1;
                }
            }

            if (counter == 2) 
            {
                primeNumbers = primeNumbers + i + " ";
            }
        } 

        System.out.println(primeNumbers);
    }
}
seyon__t
  • 15
  • 4
  • Please [edit] your question to format your code. It is very hard to read as is – GBlodgett Dec 07 '18 at 22:42
  • You'll get better help if you try something first. – Jonathan Wilson Dec 07 '18 at 22:44
  • 2
    That said, maybe you should be adding your prime numbers to a collection (such as [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html)), rather than to a string. That way, you can later iterate over the collection strategically for display purposes. – Jonathan Wilson Dec 07 '18 at 22:45
  • Can't use arrays :( @JonathanWilson – seyon__t Dec 07 '18 at 23:01
  • Why can't you use arrays? Does somebody out there want you to feel the pain of their absence? – Jonathan Wilson Dec 07 '18 at 23:03
  • LOL for this exercise we can't, according to my prof. @JonathanWilson – seyon__t Dec 07 '18 at 23:06
  • Keeping track of whether a number is prime or not by `counter` is probably not the best way. Why not use a boolean variable? – GBlodgett Dec 07 '18 at 23:07
  • @GBlodgett Agreed that the use of `counter` is questionable. That said, we will need a variable to keep track of how many primes we've found as we find them. Then, as we find each prime, we can simply print to the screen with `System.out.print`. On every 10th number (ie. `foundPrimeCount % 10 == 0` we use `System.out.println` instead. – Jonathan Wilson Dec 07 '18 at 23:10

3 Answers3

0

You need to count how many items you already added and once you have 10 you need to put new line. Also I changed String to StringBuilder because concatenating in a loop is not very good, you can read about it here StringBuilder vs String concatenation

int i = 0;
int num = 0;
int lineCounter = 0;

StringBuilder primeNumbers = new StringBuilder();

for (i = 1; i <= 230; i++) {
    int counter = 0;

    for (num = i; num >= 1; num--) {
        if (i % num == 0) {
            counter = counter + 1;
        }
    }

    if (counter == 2) {
        primeNumbers.append(i).append(" ");
        lineCounter++;
    }

    if (lineCounter == 10) {
        primeNumbers.append(System.lineSeparator());
        lineCounter = 0;
    }
}

System.out.println(primeNumbers);
Ulad
  • 1,083
  • 8
  • 17
0

Just add this piece of code below after this line in your code: primeNumbers = primeNumbers + i + " ";

    if (newLineCount == 10) {
      primeNumbers += '\n';
      newLineCount = 0;
    }
    newLineCount++;

Also init newLineCount before the loop: int newLineCount = 0;

Additionally, as mentioned in the comments, consider using StringBuilder instead of String, or even better ArrayList to store your numbers, then you can have method to print values from your ArrayList in whatever formatted way you want (with tabs, alignment, new lines...)

Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28
0

Here is the code that suits your needs. I haven't changed anything in your code, just added mine to suit your needs.

public class print_prime_numbers_10_per_line {


    public static void main(String[] args) {
        int i = 0;
        int num = 0;

        String primeNumbers = "";

        for (i = 1; i <= 230; i++) {
            int counter = 0;

            for (num = i; num >= 1; num--) {
                if (i % num == 0) {
                    counter = counter + 1;
                }
            }

            if (counter == 2) {
                primeNumbers = primeNumbers + i + " ";
            }
        }
        String[] integerStrings = primeNumbers.split(" ");

        int[] integers = new int[integerStrings.length];
        for (int x = 0; x < integers.length; x++) {
            integers[x] = Integer.valueOf(integerStrings[x]);
        }

        for (int g = 0; g < integers.length; g++) {
            if (g % 11 == 0) {
                System.out.println();
            } else {
                System.out.print(integers[g] + " ");

            }
        }
    }
}
MS90
  • 1,219
  • 1
  • 8
  • 17