Solved

Building Array....Compile error.

Posted on 2004-09-22
7
219 Views
Last Modified: 2010-03-31
--------------------------------------------------------------------------------------------------
Code begin
--------------------------------------------------------------------------------------------------
/*
instance variables
-----------------------------------------
private double[][] M                  array representing the Matrix

private final int MAX_SIZE             constant representing the maximum size of a Matrix;
                        it should be set to 100
-----------------------------------------

The Code:
public methods
-----------------------------------------
Matrix(int sizeN)                  A constructor that creates a Matrix of
                        size N x N filled with zeros
                        N must be in the range [0, MAX_SIZE].
                        If N is negative, create a Matrix of size 0 x 0.
                        If N is larger than MAX_SIZE, create a Matrix
                        of size MAX_SIZE x MAX_SIZE.

Matrix(int sizeN, double[][] initValues)      A constructor that creates a Matrix of size N x N
                        and fills it with the values found in the array initValues.
                        N must be in the range [0, MAX_SIZE].  If N is negative,
                        create a Matrix of size 0 x 0.  If N is larger than MAX_SIZE,
                        create a Matrix of size MAX_SIZE x MAX_SIZE.
-----------------------------------------
*/

public class Matrix
{
      //initialize array M
      private double[][] M;

      //variable to set max size of array
      private final int MAX_SIZE = 100;

      //Constructor One
      //Create a new Matrix, initializing it to zeros
      //The size must be a value in the range [0, MAX_SIZE]
      public Matrix(int sizeN)
      {
            if (sizeN > MAX_SIZE)
            {
                  System.out.println("The maximum size of a matrix is " +
                        MAX_SIZE);
                  sizeN = MAX_SIZE;
            }
            else if (sizeN < 0)
            {
                  System.out.println("The size of a matrix cannot be negative");
                  sizeN = 0;
            }

            //build array M with sizeN x sizeN initalized to 0
            M = new double[sizeN][sizeN];
            for (int i = 0; i < sizeN; i++)
                  for (int j = 0; j < sizeN; j++)
                        M[i][j] = 0.0;
      }

      //Constructor Two
      //Create a new Matrix, initializing it with the above parameters
      //The dimensions must be in the range [1, MAX_SIZE]
      public Matrix(int sizeN, double[][] initValues)
      {
            initValues = new double[sizeN][sizeN];
            double fillArray;
            for (int i = 1; i < MAX_SIZE; i++)
                  fillArray = 0;
                  for (int j = 1; j < MAX_SIZE; j++)
                        fillArray += initValues [i][j];
      }
--------------------------------------------------------------------------------------------------
code end
--------------------------------------------------------------------------------------------------


Problem: (Compile error)
--------------------------------------------------------------------------------------------------
cannot resolve symbol
symbol  : variable i
location: class Matrix
fillArray += initValues [ i ] [ j ];
                                 ^

What's the problem?  Up to "//Constructor Two" the code compiles without error.
As you probably know I'm writing a "matrix" class.
Last do you see any flaws with the rest of the build either in code itself or my approach?

Thanks.
0
Comment
Question by:smkJackstraw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 100 total points
ID: 12129653
Try putting brackets around the body of that last for loop...
0
 

Author Comment

by:smkJackstraw
ID: 12129705
You know....syntax errors will be the death of me.  It worked!  Before I award the points, would you answer this last question:

Why didn't I get the syntax error with the previous loop:  //build array M with sizeN x sizeN initalized to 0

There nearly identical.  Which is more 'proper' syntax? (with reguards to Java AND the idea that I'm learning Java as a stepping stone to C++)

0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12129771
Unfortunately, the Java compiler often doesn't catch errors with brackets and loops/if-statements (I've had similar issues when I've forgotten brackets, though with an if-statement). I always use brackets and I have never seen it done without them. The reasons for this are:
1. No chance of having the compiler miss it and give you some other error/get a run time error.
2. It's easier to follow. If you can see where every loop begins and ends with two clearly defined markers it makes it easier to skim the code (an important skill when you start writing long code). It's also easier to follow for others who may read your code. :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 12129779
> Why didn't I get the syntax error with the previous loop:  //build array M with sizeN x sizeN initalized to 0

Because there was only a single statement in the loop, so brackets were optional.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12129780
P.S. I'd add brackets to the previous "//build array M with sizeN x sizeN initalized to 0" to make sure there's no confusion. Even if it runs correctly now, you may end up doing some editing to find it not working later.
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12129788
>>Because there was only a single statement in the loop, so brackets were optional.

i.e. The compiler sees this:
          for (int i = 1; i < MAX_SIZE; i++){
               fillArray = 0;
          }
          for (int j = 1; j < MAX_SIZE; j++){
               fillArray += initValues [i][j];
          }
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12129824
:)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
Way to decrease size of apk file 9 109
ejb wildfly example 2 74
Need help aligning JRadioButton that put in a box using GridBagLayout 2 27
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question