Sign in to follow this  

How to use the % sign in Java - printf

This topic is 3859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am trying to use this statement in Java...
System.out.printf("%d % %d = %d", int1, int2, int1%int2);
//assuming int1, int2 are inputed by the user.
I am new to Java, just started summer semester, and I couldn't find anything related to this issue on google, or in my text book, so I thought I would come here. Obviously the problem is the '%' character in the printf string. I have no idea how to write this properly. I tried '"%d "+"%"+" %d = %d"' I also thought maybe it was an 'escape character' so I tried \% but nothing works. Thanks for any help in this! BUnzaga [edited for id-10-t errors]

Share this post


Link to post
Share on other sites
Here is the full code if it will help at all...


//CS1400-001
//Brett Unzaga
//Homework1
//Version 1.4
/*Add, subtract, multiply, divide, find remainder
of two numbers inputed by the user, then output
the results to the user.*/


import java.util.Scanner;

public class Homework1
{
public static void main(String args[])
{
Scanner userInput=new Scanner(System.in);
int num1;
int num2;
System.out.print("Enter the first integer: ");
num1=userInput.nextInt();
System.out.print("Enter the second integer: ");
num2=userInput.nextInt();
doMath("%d + %d = %d\n",num1, num2, num1+num2);
doMath("%d - %d = %d\n",num1, num2, num1-num2);
doMath("%d * %d = %d\n",num1, num2, num1*num2);
doMath("%d / %d = %d\n",num1, num2, num1/num2);
doMath("%d mod %d = %d\n",num1, num2, num1%num2);
}//close method 'main'

public static void doMath(String problem, int number1, int number2, int result)
{
System.out.printf(problem, number1, number2, result);
}//close method 'doMath'
}




I'd like to change the line:
doMath("%d mod %d = %d\n",num1, num2, num1%num2);

with something like:
doMath("%d % %d = %d\n",num1, num2, num1%num2);

To stay consistant with the rest of the code.

Share this post


Link to post
Share on other sites
This doesnt really look java-ish, but its probably your first (or one of your first) porgrams and you didnt get OOP yet.

Can anyone tell me what %d does? I never ever used that before (didnt miss it)...

Or is it strictly printf?

Edit: already found out.

Share this post


Link to post
Share on other sites

String s = "Result: " + num1 + " % " + num2 + " = " + (num1 % num2);
System.out.println(s);




String concatenation is straight-forward in Java, so there's no need for printf in your case.

For more complex tasks you have classes like DecimalFormat.

System.out.printf is somewhat recent addition to Java.

Share this post


Link to post
Share on other sites
And besides that Antheus' example is a lot clearer for both you and other people reading the code at a later point. That's my opinion, though...

Share this post


Link to post
Share on other sites

This topic is 3859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this