bruno smith

Greenhorn
+ Follow
since Jan 23, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by bruno smith

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
12 years ago

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?
12 years ago

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]
    12 years ago

    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 :/
    12 years ago

    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..
    12 years ago
    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]
    12 years ago