3 Replies - 133 Views - Last Post: 05 February 2013 - 05:53 PM Rate Topic: -----

#1 _RossTheBoss_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-January 13

Find end of a print line

Posted 05 February 2013 - 02:59 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Find end of a print line

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Find end of a print line

Posted 05 February 2013 - 03:04 PM

There are at least 2 ways, but the way I've seen the least is to print the first number:

1

then print every number remaining, preceded by a comma and a space:

, 3, 5, 7

so that it would look something like:

System.out.print( ", " + myArray[i] );

The other way involves an 'if' statement after every number in the sequence is printed.
Was This Post Helpful? 3
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,881
  • Joined: 19-March 11

Re: Find end of a print line

Posted 05 February 2013 - 03:21 PM

Quote

There are at least 2 ways, but the way I've seen the least is to print the first number:

1

then print every number remaining, preceded by a comma and a space:

, 3, 5, 7

so that it would look something like:

System.out.print( ", " + myArray[i] );


You might think about abstracting this functionality and keeping it around in a personal utilities class. It's a common method in many languages, usually called "join" ("implode" in PHP)

It's an easy method to write, but it's fun and useful to assemble your own personal toolkit. Good practice, as well.
Was This Post Helpful? 0
  • +
  • -

#4 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Find end of a print line

Posted 05 February 2013 - 05:53 PM

System.out.print(i );	
if(i<myPrimes.length-1)System.out.print(" ,");


Add this , if check, to the related portion of your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1