Reputation: 187
I am writing a program where 2 integers are inputted by user,then the program determines whether the first is a multiple of the second and prints the result.
Here is my code:
package assignmentsmodule1_exercise2.pkg16;
import java.util.Scanner;
public class AssignmentsModule1_Exercise216 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number1;
int number2;
System.out.print("Enter first number: ");
number1 = input.nextInt();
System.out.print("Enter second number: ");
number2 = input.nextInt();
if (number1 % number2 == number1);
System.out.printf("%d % %d%n", number1, number2);
}
}
Every time I run the program an error message comes up.
Here is the error code:
Exception in thread "main" java.util.IllegalFormatFlagsException: Flags = ' '
at java.util.Formatter$FormatSpecifier.checkText(Formatter.java:3037)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2733)
at java.util.Formatter.parse(Formatter.java:2560)
at java.util.Formatter.format(Formatter.java:2501)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at assignmentsmodule1_exercise2.pkg16.AssignmentsModule1_Exercise216.main(AssignmentsModule1_Exercise216.java:34)
Java Result: 1
Is there anyone who can help me with my problem?
Upvotes: 0
Views: 256
Reputation: 12748
You haven't escaped your % character on your printf function. The % alone is not permitted, Java tries to combine it to the next space character. Use another % to escape ie. write %% instead.
Source: Java: Literal percent sign in printf statement
Upvotes: 0
Reputation: 187
Your printf needs to escape the '%' character:
System.out.printf("%d %% %d%n", number1, number2);
Just add an extra '%' to escape it.
Upvotes: 0
Reputation: 8562
your printf statment has has unescapped % sign. Your multipliciyt test is also wrong.
Upvotes: 0
Reputation: 150138
M is divisible by N when M % N == 0
Your line
if (number1 % number2 == number1);
should be
if (number1 % number2 == 0)
(Notice I removed the ;
mentioned by @Sotirios in his comment.
Also you need to escape the literal % in the following line
System.out.printf("%d % %d%n", number1, number2);
Should be
System.out.printf("%d %% %d%n", number1, number2);
Also, you have three placeholders but only two parameters.
Upvotes: 4