0

I am currently learning Java and am having trouble with some code

I tried messing around with the syntax of it but I have no idea.

class Main {
  public static void main(String[] args) {
    System.out.printf("%14s%n", "Location: Key West, Florida");
  }
}

I want it to indent but nothing is happening

Mike G
  • 4,232
  • 9
  • 40
  • 66
  • 1
    Possible duplicate of [Align printf output in Java](https://stackoverflow.com/questions/15961130/align-printf-output-in-java) – Kaan Oct 01 '19 at 19:55

2 Answers2

3

It's because %14s assigns a space of 14 character and your string is longer than that so it seems as no effect.
If you want to see the effect try

System.out.printf("%34s%n", "Location: Key West, Florida");

It will give a space of 34 character and Right - Indent your string.
If you want a Left - Indent add a - before the size of formatting.

System.out.printf("%-34s%n", "Location: Key West, Florida");
CaffeinatedCod3r
  • 821
  • 7
  • 14
-2

You can use the \t character to add the tab-space character to a printed string. e.g.

 System.out.printf("\t%s", "public class Test {\n \t\tSystem.our.println(\"it works fine\");\n \t}");

OUTPUT:

public class Test {
    System.our.println("it works fine");
}
Multihunter
  • 5,520
  • 2
  • 25
  • 38
snipershady
  • 197
  • 1
  • 7