Home > Net >  java format string printing unable to print
java format string printing unable to print

Time:02-10

I'm unable to run the code, it says it cannot resolve println with the parameters.

public void run() {
        for (int i = 0; i < 10; i  ) {
            System.out.println("%s : %d", this.getName(), i);
        }
    }

CodePudding user response:

System.out.println just prints, literally, a string, and then prints a newline.

You want System.out.printf, which prints 'format strings' as you're using here. Note that it does not print newlines. Just add \n, %n doesn't really do what you think it does (it would print e.g. \r\n on windows. This is unneccessary and sometimes even actively harmful, hence, don't do that unless you really know you need it. You need it if you're emitting text files that need to look good when opened with notepad.exe. That's... about the one and only thing on the planet that requires \r\n to function. Everything else either accepts both \r\n, or only accepts \n, hence, just.. write \n, not %n).

Thus:

System.out.printf("%s : %d\n", this.getName(), i);

CodePudding user response:

That is because there is no println() method that takes a format string and substitution parameters.

There is the format() method though (when using that, remember that you have to add the newline char in the line yourself).

Where: the real answer is ... in programming, every character matters. Meaning: you have to be really diligent about such things. Always read the javadoc carefully, and when you are taught: "there is no such method", then: look at the javadoc for the class you intend to use. Ensure that the method you want exists, and then ensure that the arguments you provide to it are matching the documented method signature.

CodePudding user response:

System.out.println does not support format parameters. You can either use System.out.printf to immediately format and then print or format it with String.format which will give you a String result that you can then print or store into a variable.

System.out.printf example:

for (int i = 0; i < 10; i  ) {
        System.out.printf("%s : %d%n", this.getName(), i);
}

You would need to add %n to the formatting in order to print on new lines shown above.

String.format example:

for (int i = 0; i < 10; i  ) {
    System.out.println(String.format("%s : %d", this.getName(), i));

    //can also create a String variable with the result
    String formattedStr = String.format("%s : %d", this.getName(), i);
    //do whatever you want with the String
}

You would not need to add %n in this case because println will add the new line.

CodePudding user response:

[IGNORE PLESE] The System.out.println() method only takes one argument, so if you want to join the two parts together, you could do something like

System.out.println("%s : %d "   this.getName()   " "   i);

Fyi, instead of this.getName() you could just use getName() in this situation (unlike in e.g. javascript).

  •  Tags:  
  • Related