How to print the value of variable in java
How to print the value of variable in java
If System.out.printf
is giving you this error:
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
Then you must configure your project for the proper Java version.
Methods with variable arguments were introduced with Java 5.
Alternatively, you could do:
System.out.printf(The Sum of %d and %d is %dn, new Object[] {a, b, sum});
Have look at this question for your error: Eclipse Java printf issue PrintStream is not applicable
Alternatively you can use .format
System.out.format(The sum of %d and %d is %d . ,1, 2, 3);
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
With proper Java version set .printf
also works as expected
System.out.printf(The sum of %d and %d is %d . ,1, 2, 3);
How to print the value of variable in java
int a = 2;
int b = 4;
int c = a + b;
System.out.format(The sum of %d and %d is %d , a, b, c);
This is simple way in the formatted output