Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Find end of a print line

$
0
0
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.
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

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>