Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 624991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:15:39+00:00 2026-05-13T19:15:39+00:00

I’m trying to write a program which prompts the user to enter two 3×3

  • 0

I’m trying to write a program which prompts the user to enter two 3×3 matrices and displays their product.

For example, a user can enter:

Matrix A: 2 4 6 8 10 12 14 16 18
Matrix B: 1 2 3 4 5.6 6.6 7.4 8.1 9

Below is what I have tried, but I keep getting this error. Any help to point me in the right direction would be appreciated. I’m trying to get it to one decimal place:

Exception in thread "main" java.util.IllegalFormatPrecisionException: 2
    at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2892)
    at java.util.Formatter$FormatSpecifier.(Formatter.java:2643)
    at java.util.Formatter.parse(Formatter.java:2480)
    at java.util.Formatter.format(Formatter.java:2414)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at Exercise6_25.main(Exercise6_25.java:55)

import java.util.Scanner;

public class matrixCalc
{
    public static void main(String args[])
    {
        Scanner s= new Scanner(System.in);

        int i,j,k;
        int n=3;

        double a[][]= new double[n][n];
        double b[][]= new double[n][n];
        double c[][]= new double[n][n];

        System.out.println("enter the array elements of a:");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                a[i][j]=s.nextDouble();
            }
            System.out.print(" ");
        }

        System.out.println(" "); 
        System.out.println("enter the array elements of b:");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                b[i][j]=s.nextDouble();
            }
            System.out.print(" ");
        }

        System.out.println(" ");
        System.out.println("the result matrix is:");

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                for(k=0;k<n;k++)
                {
                    c[i][j]+=a[i][k]*b[k][j];
                }
            }
        }

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.printf("%.2d", c[i][j]+" ");
            }
            System.out.println();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:15:39+00:00Added an answer on May 13, 2026 at 7:15 pm

    You are using the %d specifier, which requires an integer argument — but you are giving it a String (because c[i][j]+" " converts c[i][j] to String when concatenating it).

    Also, the %d specifier does not use a decimal point at all. Since integral types can be implicitly converted to floating-point types, the %f specifier is what you’re looking for.

    And finally, the number after the decimal point in the format specifier is what tells it how many decimal places to go to. You say you only want one decimal place, so let’s make that a 1.

    So what we end up with is this:

    System.out.printf("%.1f ", c[i][j]); 
    

    See the Formatter Javadocs for a (somewhat mind-boggling) description of the all possible format specifiers. (Don’t worry too much if you can’t understand everything there; you’ll never need most of it anyway.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 370k
  • Answers 370k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This link was helpful. Very annoying that Microsoft changed the… May 14, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer The "edit" button in MySQL Administrator will use the primary… May 14, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer Not sure quite what you're asking; Wikipedia has pseudo-code: http://en.wikipedia.org/wiki/SHA-1… May 14, 2026 at 6:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.