-5

I am trying to align my output so that it looks like this:

11
13
15
17 

Right now, it only prints out like 11,13,15,17. What should I do with my code to make it properly aligned? I tried adding \n after true which didn't work.

Code:

boolean test {
    
    int z, 
    for (z = 10, i <= 30; i++) {
        if ((z % z== 0) && (z % 1 == z))
            }               
        System.out.println(true); //number is prime
        System.out.println(false);//number is not prime
Community
  • 1
  • 1
Kal
  • 9
  • 3
  • 4
    `z % z== 0` doesn't make much sense, since the result will always be 0. And `z % 1 == z` will always be 0 as well, so it will never be equal `z` – pzaenger Aug 23 '14 at 12:16
  • It also won't compile, since the `if`'s condition is immediately followed by a closing curly. – yshavit Aug 23 '14 at 12:17

2 Answers2

2

You should use System.out.print() instead of System.out.println() for the integer, and println() for the boolean.

PrintSteam.println() ends the printing with a terminal newline '\n'.

See this:

int z;
for (z = 10; z <= 30; z++) {
    System.out.print(z);
    System.out.print(' '); // Add spacing between number and boolean.
    if ((z % z == 0) && (z % 1 == z))
        System.out.println(true); //number is prime
    else
        System.out.println(false);//number is not prime
}

Here is an online ideone code demo!

But you can also print in a simpler loop:

for (int z = 10; z <= 30; z++)
    System.out.printf("Is %s prime? %s\n", z, (z % z == 0) && (z % 1 == z));

However the algorithm for checking if it's a prime is incorrect, you can use a modified method from this thread:

for (int z = 10; z <= 30; z++)
    System.out.printf("Is %s prime? %s\n", z,
            !new String(new char[n]).matches("(..+?)\\1+"));

In source code of PrintStream.println():

/**
 * Prints a boolean and then terminate the line.  This method behaves as
 * though it invokes <code>{@link #print(boolean)}</code> and then
 * <code>{@link #println()}</code>.
 *
 * @param x  The <code>boolean</code> to be printed
 */
public void println(boolean x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}
Community
  • 1
  • 1
Nordehinu
  • 338
  • 1
  • 3
  • 11
  • like this? System.out.print(true \n); – Kal Aug 23 '14 at 12:23
  • Why you recommend using `print` instead of `println` when he wants every entry on his own line and not next to each other? – Tom Aug 23 '14 at 12:25
  • @Nordehinu you may edit your first sentence :). And add a delimiter after printing the number. `11false` doesn't look good :). – Tom Aug 23 '14 at 12:37
1

You want to revise your code as shown below. You should have been checking against "i" not "z"

public class TestPrime {

    public static void main(String[] args) {
        for (int z = 1; z < 40; z++) {
            if (isPrime(z)) {
                System.out.println(" " + z + " is prime");
            }
        }
    }

    public static boolean isPrime(int z) {
        boolean isPrime = true;
        for (int i = 2; i <= 30 && i < z; i++)
            if ((z % i) == 0 && z != i) {
                isPrime = false;
                break;              
            }
        return isPrime;
    }
}
ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37