My task is to output the results of a certain operation to the console, and I used this approach:

System.out.println(result + "text" + "eshoPeremennaia" + "text i tak dalee");

But I was answered for this case: нельзя складывать строки. используй String.format. нельзя складывать строки. используй String.format.

Tell me how it is solved with the help of String.format where I don’t look about this method everywhere they tell how to restrict the decimal points or how to move the string ... Maybe someone knows how to bring this to the console without folding the strings ..?

  • Is result just a variable? or is there some kind of additional text? - Grundy

1 answer 1

Information on this feature can be found in the help.

Applied to the example from the question, the call might look like this:

 System.out.println( String.format("%1$d text %2$d text i tak dalee", result, eshoPeremennaia) ); 

example on ideone

  • Exception in thread "main" java.util.IllegalFormatConversionException: d! = Java.lang.String gives this error - Pavel
  • @Pavel, it all depends on the types of variables and their values - Grundy
  • 2
    @Pavel, well, you read the certificate, to which you were given a link. Who knows what type of variables you wrote in your example - you just gave a passage. For whole, you can use %d , for strings - %s , for real heaps of their formats are there, for dates and times too, and 1$ 2$ are rarely needed. Read, do not be lazy, in a nutshell this topic can not be described. And in Russian is full of materials on this topic. - m. vokhm
  • @Paul, most likely you just need to replace d with s if strings are stored in variables - Grundy
  • And sorry, I was wrong, you are right, I really have the same type. Thank! - Pavel