Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Doble array execution

Posted on 2004-04-27
15
Medium Priority
?
362 Views
Last Modified: 2009-12-16
I am trying to find the array that results from the execution of (3, 4, 21)
Where the code for the method that was given to me is:

public static int [] []  f(int n, int m, int value);
{
        int [] [] x = new int [n] [m];
        for (int i=0; i < x.lenght; i++)
        for (int j = 0; j < x[i].lenght; j++)
        x[i] [j] = value;
        return x;
}
      

Down below you can find how tried to run it.... I am missing something but do not know what.  Can someone help me with this?  Thank you!   J.Cisek - Java student.


public class SkillBuilder
{

    int [] [] f(int n, int m, int value);
   

    public static void main(String[] args)
    {
       
        int [][] x = new int [n] [m];
        for (int i=0; i < x.lenght; i++)
        for (int j = 0; j < x[i].lenght; j++)
        x[i] [j] = value;
        return x;
        }

    System.out.println( x (x(int n, int m, int value) ) );
0
Comment
Question by:j_cisek
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10932652
You can't return something from a method that declares that it returns void.

You can't ever return anything from main(). Put the code you have written in a method with a different name than main() and make sure it returns in[][]. Furthermore, there are no prototype declarations in Java (Java is not C ;-).
0
 
LVL 7

Expert Comment

by:mark-b
ID: 10932699
You also have to spell 'length' correctly. :)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10932700
Also, System.out.println is not defined for int[][].
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10932739
This should get you on your way:

      public static int[][] populateArrays(int n, int m, int value) {
            int[][] x = new int[n][m];
            for (int i = 0; i < x.length; i++) {
                  for (int j = 0; j < x[i].length; j++) {
                        x[i][j] = value;
                  }
            }
            return x;
      }
0
 
LVL 7

Accepted Solution

by:
mark-b earned 2000 total points
ID: 10932772
probably what you want is something like this:

public class SkillBuilder {

    private static int [] [] f(int n, int m, int value) {
        int [][] x = new int [n] [m];
        for (int i=0; i < x.length; i++) {
         for (int j = 0; j < x[i].length; j++) {
            x[i] [j] = value;
         }
        }

        return x;
    }
   
    public static void main(String[] args)  {
      int[][] x = f[Integer.parseInt( args[0] ), Integer.parseInt(args[1] ), Integer.parseInt(args[2] ) );
   
      // do something with x
    }
0
 

Author Comment

by:j_cisek
ID: 10932954
Dear orangehead911,

Thank you for your comments there are very useful, especially that you remind me that I have to be more detail oriented.  My next question is how to print out the resultant array.
Do I have to write a String toString() method to print it?  If yes, can this method be inside of a public static void main(String[] args)?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10933031
You can slightly simplify the first part. Here also is a toString:


      private static int[][] f(int n, int m, int value) {
            int[][] x = new int[n][m];
            for (int i = 0; i < x.length; i++) {
                  Arrays.fill(x[i], value);
            }
            return x;
      }


      public String toString(int[][] array) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; i++) {
                  for(int j = 0; j < array[i].length - 1; j++) {
                        sb.append(array[i][j]);
                        sb.append(",");
                  }
                  sb.append(array[i][array[i].length - 1]);
                  sb.append("\n");
            }
            return sb.toString();
      }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10933039
(import java.util.Arrays)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10933325
>> Do I have to write a String toString() method to print it?
The toString() method is a method you would override in a class that you've implemented to provide useful information about the state of the class instance. In your case you don't have a class, so the answer is no. You would want to implement functionality similar to toString(). Why not use the code posted by CEHJ above?!?

>> If yes, can this method be inside of a public static void main(String[] args)?
You can never place a method definition within another method definition(*). You can define a method for the printing of the values outside the main method (must also be static) and call that method from inside your main method.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10933337
What a nice point split!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10933359
>>What a nice point split!

Yes. Looks like some have come out of that rather badly ;-)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10933538
Indeed, I guess that's what you get for providing good help eh? =-)
0
 
LVL 7

Expert Comment

by:mark-b
ID: 10942232
j_cisek,

Could you please reopen this question it and distribute the points 3-ways between CEHJ, orangehead911, and myself?

Thanks,

-Mark
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 11045511
I guess that's not happening! :-(
0
 
LVL 7

Expert Comment

by:mark-b
ID: 11045553
Guess not.  Sorry guys!
0

Featured Post

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!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

885 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