-2

The code shows:

java.util.IllegalFormatConversionException: d != java.lang.String at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302) at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747) a

public class addiePorterMod10Sieve {
    void sieveOfEratosthenes(int n) {
        boolean prime[] = new boolean[n + 1];
        for (int i = 0; i < n; i++)
            prime[i] = true;
        for (int p = 2; p * p <= n; p++) {
            if (prime[p] == true) {
                for (int i = p * p; i <= n; i += p)
                    prime[i] = false;
            }
        }
        for (int i = 2; i <= n; i++) {
            if (prime[i] == true)
                System.out.printf(i + "%-1s %-15n", " ");
        }
    }

    public static void main(String args[]) {
        int n = 1000;
        addiePorterMod10Sieve g = new addiePorterMod10Sieve();
        g.sieveOfEratosthenes(n);
    }
}
Januson
  • 4,533
  • 34
  • 42
  • Please add more information such as the error you are getting. – Januson Feb 21 '19 at 14:27
  • Will you tell us the error? – achAmháin Feb 21 '19 at 14:27
  • just added the error in the comment section above the code. @Januson – Addie Porter Feb 21 '19 at 14:29
  • Your problem is this line : `System.out.printf(i + "%-1s %-15n", " "); ` – nullPointer Feb 21 '19 at 14:31
  • What would I change it too? I need a space between each integer and a new line within every 15 integers. – Addie Porter Feb 21 '19 at 14:33
  • The question being asked here is really this: https://stackoverflow.com/q/3179504/1531971 How to fix the error is to look at the stacktrace thrown at runtime and then read the docs on Java `printf` in the API docs. –  Feb 21 '19 at 14:37
  • posted code is probably not the problem.... there must be some `%d` in a `printf` But, using `printf` together with string concatenation is as *strange* as `if (expr == true)` – user85421 Feb 21 '19 at 14:55

2 Answers2

1

Not very familiar with formatter myself, but the below workaround should achieve what you need as ouput :

int linecount = 0;
for(int i = 2; i <= n; i++) 
{          
     if(prime[i] == true)  {
      //   System.out.printf(i + "%-1s %-15d", " "); 
           linecount++;                   
           System.out.print(i + " ");
           if (linecount == 15) { 
              linecount =0;
              System.out.println();
           }
      }
}
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

The error derives from %'s matching with a parameter.

    int p = 0;
    String nl = "\r\n";
    for (int i = 2; i <= n; i++) {
        if (prime[i]) {
            ++p;
            System.out.printf("%-15d ", i);
            if (p % 10 == 0) {
                System.out.println();
            }
            //System.out.printf("%-15d%s", i, (p % 10 == 0 ? nl : " "));
        }
    }

Now %n would indeed yield a newline ("\r\n" on Windows, "\n" on Linux) with a flush of the line. However you must place it in the format string,

My out-commented alternative misses immediate flushing to the console.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138