• Post Reply Bookmark Topic Watch Topic
  • New Topic

extracting a value from an array  RSS feed

 
Jon Thor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to fetch a value 18 from the 0 array in ItemData[][] to use in main. This is as far as I am getting and I am getting kicked in the teeth.

I just started learning this stuff a few weeks ago, so I may be doing something unforgivably wrong... But if someone can point out what it is I would be grateful.


Thanks
Kald
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

You're confused on parameters and local variables, which is perfectly understandable for a beginner.

You've defined the array as a local variable in your StatFetcher method, but then you try to index an array that doesn't exist to pass a parameter. What you can do is pass two ints as parameters, and let your method do the indexing. Like:



Either that or you could define the array outside of the method, like as static member data of the class, or as a local variable in your main method.

Finally, keep in mind: methods and variables begin with a lowercase letter. Classes start with uppercase. Always indent properly. If you get used to that right at the beginning, it will make your code much easier to read for experienced Java programmers ... including yourself when you go back and look at it in six months!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you got everything static? If you search this forum, you will find several discussions about static or otherwise, several weeks ago. If you haven’t got a good reason for making something static, chances are it’s a mistake.
You are all confused about types passed to methods. I can’t understand what you are trying to do, but your method asks for an array of arrays of ints, and you are passing an int from an array which doesn’t officially exist at that point. Maybe you are trying to get one element from those arrays, in which case maybe you are better off passing i and j (parameters for method = (int i, int j)), and getting it to return array[i][j]. But I am not sure exactly what your method is supposed to do.
And get rid of the mention of that array in the main method; in the compiler’s eyes, it doesn’t exist there.
It would be a lot easier to help if you told us more details, eg exact error messages.
 
Jon Thor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Greg, it works beautifully.

As to why I use static... I have no idea. I will be sure to read what I find here on the forum. I attended a java course where the teacher could not explain it, among other things :p And secondly, the course was not in English, I am finding out that the translations we used were not very accurate or well defined and not surprisingly I am having a hard time relearning all the concepts & definitions in the proper language.

BTW the array I prepared was much larger. After studying Java commands for a while, I felt like I was studying wheat but had no idea about bread, I needed a project that would give me enough problems to work on. So, I decided to make an old style, text based dungeon crawler. This array was a part of the items in the game!

Thanks both of you for jumping on this right away like this, I am no longer alone in the javaworld
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome. Now you have got it to work, let’s change it so nothin’ ain’t static no more. (Note cowpoke grammar is required here occasionally.)I have divided that array initialiser into eight (or ten) lines otherwise it can fall off the edges of your screen. If you don’t have 19 numbers, you will suffer an Exception by using 18.
Note that item data is now a private field of that class, and you are accessing it via a non-static method call. You can make all sorts of changes to that class, and can collapse the main method from two lines to one line. The main method is static; that is unavoidable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!