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 9075627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:54:14+00:00 2026-06-16T18:54:14+00:00

My code does an error but I don’t know how to correct it: public

  • 0

My code does an error but I don’t know how to correct it:

public class Cenas {
public static void main(String[] args) {
    //gera matrizes de tamanho aleatório com uns e zeros
    int a = 2;//(int)(Math.random() *3) + 1;
    int b  = 2;//(int)(Math.random() *3) + 1;
    int[][]matriz = new int [a][b];
    do{
        for (int i=0; i<a; i++) {
            for (int j=0; j<b; j++) {
                matriz[i][j] = (int) Math.round(Math.random());
                System.out.print(matriz[i][j] + " ");
            }
            System.out.println("");
        }
    }while(matrizIdentidade(matriz)==true); //the error is in here!!! the ";"


public static boolean matrizIdentidade (int[][]m){
    boolean diagonal = false;
    if (m.length==m[0].length) //matriz.Testada[0] é o comprimento da matriz
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                if(i==j && m[i][j]==1)
                    if(i!=j && m[i][j]==0)
                        diagonal = true;
    return diagonal;
}
}

It generates random matrices and tells me if they are an identity matrix or not. I put the System.out.print and dimension 2 by 2 just for testing. The error makes my loop infinite…

“;” on the line commented appears underlined in red (in Eclipse) giving me an error.

I humbly think that you are missing my question. I don’t know if the statements in my methods are correct (I’m working on it), but what brings me here is that the “;” gives me an error: “Syntax error, insert “}” to complete MethodBody”. If that is due to my badly coded logic I apologized. But I think, instead, that indicates that I’m doing some syntax error on the do-while loop.

  • 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-06-16T18:54:15+00:00Added an answer on June 16, 2026 at 6:54 pm

    You are defining the return value only by the last member

      diagonal = true;
    

    Should be the opposite, you start assuming the matrix is the identity and return false when you check that it is not true.

    if((i==j && m[i][j]!=1) || (i!=j && m[i][j]!=0)) {
       // this is not an identity matrix, so you can stop
       return false;
    }
    

    if the loop ends, the matrix is the identity so return true.

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

Sidebar

Related Questions

This code does not throw an error but the query fails, that is, the
Why does following code snippet doesn't work? It doesn't gives any error, but also
I am puzzled, simple code fails to return error for PIPE2 open, but does
Why does the following code give me an error (g++ 4.1.2)? template<class A> class
Does anyone know what this error message means? FATAL: Code generation error detected during
Question: I receive the following error for the code below, does anyone know why?
I don't know why I am receving this error but it keeps stating that
The following code does not compile and I don't know why: #include <type_traits> //
attrubuI'm new to python but don't know how to solve this: import wx class
The following piece of C# code does not compile: public class A { public

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.