# Find end of a print line

Page 1 of 1

## 3 Replies - 249 Views - Last Post: 05 February 2013 - 05:53 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311277&amp;s=8540255c5bc359ca6a8aab497b71d2ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 _RossTheBoss_

• New D.I.C Head

Reputation: 0
• 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

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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

• Pancakes!

Reputation: 9542
• Posts: 16,495
• 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

• D.I.C Regular

Reputation: 91
• Posts: 280
• 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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}