Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Formatting data with format

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Formatting data with format

    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
    Last edited by Norm; December 1st, 2021 at 10:08 AM. Reason: Change title

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What is wrong with my code?

    Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
    Look at the Formatter class's API doc to see what the valid format character is for a String.
    d is for decimal integer.

    The header line should be printed one time before the loop.
    The code inside of the loop would print the numbers for the table.
    The End line should be printed after the loop ends.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. wrong code?
    By makapuu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 2nd, 2018, 07:51 AM
  2. what's wrong with my code
    By vmsumalatha2002 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 1st, 2014, 05:40 AM
  3. What is wrong with my code
    By chaffee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 18th, 2013, 06:39 AM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 08:57 AM

Tags for this Thread