Reputation: 91
public static void main(String[] args) {
int num1 = 1;
int num2 = 1;
int result = num1 * num2;
System.out.println("%d x %d = %d\n",num1,num2,result);
}
I am trying to printout a form like "1 * 10 = 10". However I get an error:
The method println(int) in the type PrintStream is not applicable for the arguments (String, int, int, int)".
I don't know what's the problem and how should I change it?
Upvotes: 8
Views: 28764
Reputation: 1
System.Out.println
- does not accept arguments %d
among others
System.Out.format
- You need to use format instead of println
stays like this:
System.out.format("%d x %d = %d",num1, num2, result);
Upvotes: 0
Reputation: 1
enter code here
int first = 20;
int second = 30;
System.out.println("before swap");
System.out.println("first value :" +first);
System.out.println("second value: " +second);
first = first - second;
second = first + second;
first = second - first;
System.out.println("after swap");
System.out.println("first value: " +first);
System.out.println("second value: " +second);
remove comma from the print statement
Upvotes: -1
Reputation: 1329
Try
System.out.println(num1+" x "+num2+" = "+result+"\n");
UPDATE: Some of you are saying this concatenation method is slower than other methods. You are right, it is slower, but does it really matter for this example?
This method is usually used to debug, not as part of the final code, and usually only once or twice on the whole code.
Faster method:
System.out.printf("%d x %d = %d\n",num1,num2,result);
Upvotes: 14
Reputation: 8771
println as documented here currently accepts 0 or 1 arguments, reason for the mentioned error.
You need to either concatenate or use String Format to format the result if you intend to use println(only):
String Format:
System.out.println(String.format("%d x %d = %d\n",num1,num2,result));
String Concatenation using StringBuffer:
StringBuffer resltBuffer = new StringBuffer(num1).append(" x ").append(num2).append(" = ").append(result);
System.out.println(resltBuffer.toString());
Upvotes: 0
Reputation: 707
Have you tried using system.out.format
like this:
System.out.format("%s x %s = %s\n",num1,num2,result);
Your current solution using println
isn't working as println
cannot format text output in this way, you would have to (as others have said) concatenate the string using the "+" operator. It's slower in most cases but for debugging purposes I shouldn't imagine it's much of a problem either way.
Upvotes: 7
Reputation: 504
One of the simple way is that you can use the concatenation operation '+' instead of ',' to print
public static void main(String[] args) {
// TODO Auto-generated method stub
int result = 0;
for(int num1 = 1;num1 < 10;num1++){
for(int num2 = 1;num2 <10;num2++){
result = num1 * num2;
System.out.println(num1+"x"+num2+"="+result);
}
}
}
}
Upvotes: 4
Reputation: 1651
The Method your using (System.out.println
) isn't made for multiple parameters. By using ,
you try to give it multiple parameters. You need the +
-operator.
Applied to your code it should look like that:
System.out.println(num1 + " x " + num2 + " = " + result);
Maybe you should look at this. This is the Documentation of PrintStream. As you can see there is no method like you are using it(System.out.println(String, Int, Int ,Int);
).
Upvotes: 4
Reputation: 4430
There's a printf(...)
method in System.out
!
System.out.printf("%d x %d = %d\n",num1,num2,result);
Upvotes: 6