This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes make a method that prints array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "make a method that prints array?" Watch "make a method that prints array?" New topic
Author

make a method that prints array?

bruno smith
Greenhorn

Joined: Jan 23, 2012
Posts: 8
Hey guys im completly new to java.

I want to make an array filled with numbers on specified positions, and then i want to make a separate method that prints that array but it just wont work when im trying to call the method in main and i just cant figure whats wrong :S

here is my code:



And this is my main


uhm not much of code :O

[Edit - added code tags - see UseCodeTags]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4367
    
    8

Hi Bruno. Welcome to The Ranch!

In general, we'd ask exactly what you mean by "it doesn't work", but I can notice a few problems.

The first think I notice is the line:
Field.printField(field);
That calls a static method printField on the class Field, and passes it the reference field. There are two problems there. You don't have a Field class, and you don't have a field reference.

Also, in your createField method - you create an array called spelplan, and then start setting values in field, which doesn't exist. Are you sure that's the code you're using, or have you done a cope-and-paste and not changed everything you need to?

Anyway, back in your main method: change Field to Array and you look like you have a viable way of printing out an array once you've got it. So the first thing you need to do in the main method is create the array - or call a method that will return a newly created array.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Welcome to the Ranch!

In your first piece of code, in the createField method, you are declaring a variable (line 6) that is an array of arrays of ints. But it is a local variable inside the method. Local variables exist only inside the method. So as soon as the method ends (line 20) the variable doesn't exist anymore. In other words, in line 5 of your second piece of code, field is undefined. (Also, what is Field? You haven't defined that anywhere).

You need to store the array somewhere so that the main method can access it.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
bruno smith
Greenhorn

Joined: Jan 23, 2012
Posts: 8
Matthew Brown wrote:Hi Bruno. Welcome to The Ranch!

In general, we'd ask exactly what you mean by "it doesn't work", but I can notice a few problems.

The first think I notice is the line:
Field.printField(field);
That calls a static method printField on the class Field, and passes it the reference field. There are two problems there. You don't have a Field class, and you don't have a field reference.

Also, in your createField method - you create an array called spelplan, and then start setting values in field, which doesn't exist. Are you sure that's the code you're using, or have you done a cope-and-paste and not changed everything you need to?

Anyway, back in your main method: change Field to Array and you look like you have a viable way of printing out an array once you've got it. So the first thing you need to do in the main method is create the array - or call a method that will return a newly created array.


Hi thanks for quick response " Are you sure that's the code you're using, or have you done a cope-and-paste and not changed everything you need to?" i messed some stuff up when i translated it, im from finland so the original names wouldnt made any sense to you guys guess i missed on a few of em..
bruno smith
Greenhorn

Joined: Jan 23, 2012
Posts: 8
Jesper de Jong wrote:Welcome to the Ranch!

In your first piece of code, in the createField method, you are declaring a variable (line 6) that is an array of arrays of ints. But it is a local variable inside the method. Local variables exist only inside the method. So as soon as the method ends (line 20) the variable doesn't exist anymore. In other words, in line 5 of your second piece of code, field is undefined. (Also, what is Field? You haven't defined that anywhere).

You need to store the array somewhere so that the main method can access it.


Hi thanks for response, as i wrote i messed somethings up when i tried to translate my code to english :O sorry for making you confused. "You need to store the array somewhere so that the main method can access it." i guess this is my problem. I cant figure how to do that :/
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4367
    
    8

bruno smith wrote:i messed some stuff up when i translated it, im from finland so the original names wouldnt made any sense to you guys guess i missed on a few of em..

Fair enough - and I notice you've corrected the first post now.

So you still need to get the array into the printField method. One way would be to change createField() so that it returns the array. Call that in your main method (assigning that to a local variable) and then pass that local variable into Array.printField().

There's one other point as well, that you'll hit as soon as you're successfully calling createField(). An array of size 7 will contain indexes from 0 to 6.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Welcome to the Ranch

You should consider why you are marking all the members of your class static. It is often a mistake to mark everything static.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

Hey Bruno,
I can see some of the problems in your code
  • Field is local to the method createField, so not available outside the method itself. You need to declare array in class as instance variable.
  • You are not calling createField method anywhere then how this array can get initialized even if you declare it as instance variable.
  • Array is having 7 columns so index will varies from 0-6 so, field[0][7] = 2; is invalid.


  • Do not wait to strike till the iron is hot; but make it hot by striking....
    bruno smith
    Greenhorn

    Joined: Jan 23, 2012
    Posts: 8
    Manoj Kumar Jain wrote:Hey Bruno,
    I can see some of the problems in your code
  • Field is local to the method createField, so not available outside the method itself. You need to declare array in class as instance variable.
  • You are not calling createField method anywhere then how this array can get initialized even if you declare it as instance variable.
  • Array is having 7 columns so index will varies from 0-6 so, field[0][7] = 2; is invalid.


  • I really appreciate your help guys! To bad i seems to be to stupid to understand it :/

    "
  • Field is local to the method createField, so not available outside the method itself. You need to declare array in class as instance variable.
  • " I dont understand how to do that? Sorry for being a tard.

    "
  • Array is having 7 columns so index will varies from 0-6 so, field[0][7] = 2; is invalid.
  • " Yeah i know, it just got lost in my translation since i shortened it down. The array is acctually [9][9]
    Manoj Kumar Jain
    Ranch Hand

    Joined: Aug 22, 2008
    Posts: 191

    Just take out the "field" variable declaration out of the method like this


    now you have to call createField() method to initilize this variable. I am declaring the "field" variable as static because all methods are static so you need not to make many changes. Just take out the "field" out and declare in class itself.

    now you can call the methods by class name and dot operator

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38412
        
      23
    You are perpetuating the use of static members. Why? You should always think, “why am i writing static?” before writing static.
    bruno smith
    Greenhorn

    Joined: Jan 23, 2012
    Posts: 8
    Manoj Kumar Jain wrote:Just take out the "field" variable declaration out of the method like this


    now you have to call createField() method to initilize this variable. I am declaring the "field" variable as static because all methods are static so you need not to make many changes. Just take out the "field" out and declare in class itself.

    now you can call the methods by class name and dot operator



    Wow thanks a bunch!

    Campbell Ritchie wrote:You are perpetuating the use of static members. Why? You should always think, “why am i writing static?” before writing static.

    Why is it bad using static? If i remove all the static in the names it wont work in main?
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14114
        
      16

    bruno smith wrote:Why is it bad using static? If i remove all the static in the names it wont work in main?

    It is not necessarily bad, but as with anything when you write a program, you have to understand why you are writing it the way you are. Do you understand what static means?
    Wendy Gibbons
    Bartender

    Joined: Oct 21, 2008
    Posts: 1107

    I just looked at this and another problem, you need to look into ArrayIndexOutOfBoundsExceptions, as you will currently get one at line 10.
    an array of int[2][7] means it is 0..1 and 0..6 big
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38412
        
      23
    bruno smith wrote:. . . If i remove all the static in the names it wont work in main?
    That suggests to me you are not creating an object, which you ought to, and that you have not understood what the intent of a static member is. Yes, I know there are books which put everything in the main method and make other members static, but that isn’t object-oriented programming.

    This is object-oriented:You can now remove static from your CheckArray class. You will of course have to give it instance fields and a constructor. Note you now have two classes: one class to create the object from and the other to manipulate it in the main method. You can delete the main method from the CheckArray class.
    bruno smith
    Greenhorn

    Joined: Jan 23, 2012
    Posts: 8
    Thanks for the help so far. Maybe i should take this from the begining. This is what i want to do:

    Create a class and make an array that is [9][9] big. Then i want to give some custom values to the index. like

    array[0][0] = 5;
    array[0][2] = 9;
    array[0][7] = 1;

    array[2][2] = 3;
    array[2][4] = 4;
    array[2][5] = 2;

    array[4][2] = 1;
    array[4][7] = 3;
    array[4][4] = 2;

    etc etc..

    When i have created my array i want to make a separate method that prints the array. And after that i want to make a method that checks if a row or/and colum has a specified number (this part i can solve on my own) java just makes me confused :S
    Wendy Gibbons
    Bartender

    Joined: Oct 21, 2008
    Posts: 1107

    Bruno if I was doing it I would make a static method called createTestArray, which returned you your test array (just like the one in your example).

    Then I would try to understand Cambell Richtee's responses about static objects and making methods, as your codes seemed ok, but you were a bit confused.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: make a method that prints array?