I have a program that prints prime numbers from 0 to 200 where each number is separated by a comma and a space so that it looks like 2, 3, 5, 7, 11, etc. This is the code i have which works perfectly except one thing.
The only problem is that after the last number prints another comma prints after it. How do I get ride of the last comma? Please help, thanks in advance
public class Primes { public static void main(String[] args) { final int MAX = 200; boolean[] primes = new boolean[MAX]; fillPrimes(primes); computePrimes(primes); displayPrimes(primes); } public static void fillPrimes(boolean[] myPrimes) { for(int i = 0;i < myPrimes.length;i++) { myPrimes [i] = true; } } public static void computePrimes(boolean[] myPrimes) { for(int i = 2;i<myPrimes.length; i++) { if(myPrimes[i]) { for (int j = i+1;j<myPrimes.length;j++) { if(j%i==0) { myPrimes[j] = false; } } } } } public static void displayPrimes(boolean[] myPrimes) { for(int i = 2;i < myPrimes.length;i++) { if(myPrimes[i]==true) { System.out.print(i + ", "); /**(myPrimes[i]+ ",");**/ } } } }
The only problem is that after the last number prints another comma prints after it. How do I get ride of the last comma? Please help, thanks in advance