Solved

Doble array execution

Posted on 2004-04-27
15
357 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
[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
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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 500 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

622 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