• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

make a method that prints array?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
bruno smith
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
  •  
    bruno smith
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 198
    Oracle Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are perpetuating the use of static members. Why? You should always think, “why am i writing static?” before writing static.
     
    bruno smith
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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?
     
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79180
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 L Gibbons
    Bartender
    Posts: 1111
    Eclipse IDE Oracle VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

     
    reply
      Bookmark Topic Watch Topic
    • New Topic