1

I am trying to write a program that will list out the prime numbers up to, but not including a number determined by the user.

I want the primes to be separated by commas, but I can't seem to get rid of the comma after the final prime.

Say the user picks the number 10.

The list should be: 2,3,5,7

What I get is: 2,3,5,7,

I know this probably gets asked a lot, but I can't find anything that will help, or that I understand how to apply to my problem.

Could someone point me in the right direction?

Thanks.

Scanner scanner = new Scanner(System.in);

    int i = 0;

    int num = 0;

    String primeNumbers = "";

    System.out.println("Pick a number, any number: ");

    int n = scanner.nextInt();

    for (i = 1; i < n; 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("\nThe prime numbers up to " + n + " are: ");

    System.out.println(primeNumbers);
Keeron
  • 47
  • 1
  • 10
  • It's not a good idea to concat strings using the + operator. It's a performance bottleneck. Use `StringBuilder.append( )` and outside the for loop use `StringBuilder.delete( )` to delete the last character (comma) – denvercoder9 Nov 07 '16 at 17:55
  • Can you store the numbers in the int list? Then you can use `System.out.println(list.stream().map(String::valueOf).collect(Collectors.joining(",")));` – dty Nov 07 '16 at 17:55
  • Thanks for the help @Rafiduzmann Sonnet, I'll take your advice on board! – Keeron Nov 07 '16 at 18:09
  • Thanks @dty, I was looking at something like this but just got lost, I'll have another go though! – Keeron Nov 07 '16 at 18:11
  • See also: http://stackoverflow.com/a/41282608/363573 – Stephan Dec 22 '16 at 11:57

5 Answers5

2

Short answer: Don't add commas that you don't need.

Some Details: Invert when you add the comma.

do something like this:

for (blah)
{
    if (primenumbers.length() > 0)
    {
        primenumbers += ",";
    }

    ... blah for next prime number.

    primenumbers += nextPrimeNumber;
}
DwB
  • 35,321
  • 10
  • 55
  • 81
2

Rewrite

primeNumbers = primeNumbers + i + ",";

in the following way

if(!primeNumbers.isEmpty()) {primeNumbers+=".";}
primeNumbers = primeNumbers + i;
talex
  • 16,886
  • 2
  • 27
  • 60
0

Instead of having the comma follow the last number, you should have it precede the last number (the string can always be initialized to the first prime number without a comma).

Alternatively, you can check if the string is empty. If it isnt just remove the last char using primeNumbers = primeNumbers.substring(0, primeNumbers.length() - 1)

yitzih
  • 2,850
  • 3
  • 26
  • 42
0

An easy way to do this is to just remove the last character from the primeNumbers string.

primeNumbers.slice(0,primeNumbers.length-1);
Alissa
  • 1
  • 2
0

Just add this part before printing the number.

    if (primeNumbers.lastIndexOf(",") > 0) {
        primeNumbers = primeNumbers.substring(0, primeNumbers.length() - 1);
    }

or Just use Array List when you print use join with what separtor needed

Prakash V
  • 36
  • 3