I am supposed to create an application that will display the first five values of a multiplication table of 2. The computer must calculate the product. The generated table must be displayed as follows:
********************************************Table of 2*******************************************
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
------------------------------------------------------End------------------------------------------------------
Here is my source code:
package firstfivevaluesapp;
import java.util.Scanner;
/**
*
* @author SETH
*/
public class FirstFiveValuesApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// declare variables
int number,multiplier;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number: ");
number = input.nextInt();
input.close();
for( multiplier = 1; multiplier <=5; multiplier++ ){
System.out.printf("%d * %d = %d\n" +
"**********Table of 2**********" +"\n"+
number, multiplier, (number * multiplier)+"\n" +
"-------------End--------------");
}
}
}
Here is the output:
Enter the number:
2
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
1 * at java.util.Formatter$FormatSpecifier.failConversion (Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(F ormatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatte r.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at firstfivevaluesapp.FirstFiveValuesApp.main(FirstFi veValuesApp.java:28)
Java Result: 1