Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to use the % sign in Java - printf


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 19 May 2007 - 05:26 PM

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]

Sponsor:

#2 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 19 May 2007 - 05:32 PM

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.

#3 Forba   Members   -  Reputation: 130

Like
0Likes
Like

Posted 19 May 2007 - 05:47 PM

Use %% to to use % as a character in a string.

Eg.

System.out.printf("%d %% %d = %d", int1, int2, int1%int2);


#4 BUnzaga   Members   -  Reputation: 267

Like
0Likes
Like

Posted 19 May 2007 - 05:54 PM

Thank you SOOOOO much! I have been trying to figure it out for over an hour now.

#5 M-E   Members   -  Reputation: 109

Like
0Likes
Like

Posted 19 May 2007 - 08:45 PM

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.

#6 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 20 May 2007 - 02:04 AM


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.

#7 M-E   Members   -  Reputation: 109

Like
0Likes
Like

Posted 20 May 2007 - 03:07 AM

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...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS