• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Return and apply Method array to main method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, I'm just starting out in java, and I'm trying to figure out what I'm doing wrong, but I cant quite understand what it is I'm not catching.

Im trying to write a method that takes user inputs from the main method, apply them as the parameter into an array (2nd method) , then return it back to the main method. I cant figure out how to call it correctly, and I also need to call it more than once, so that I can use it against itself in a problem. Here is what I have:


/** Here is where I have tried numerous things to call, and I cant figure out or get anything to work. Am I naming something wrong? And how do I call it twice, or have the "n" number acknowledge into the other method as the parameter for number of entries.
 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're quite close.

1. Since the parameter for the someArray method is the desired size of the array, the parameter type should be int and not int[]. (You want to pass in an array size, not an array itself.)
2. The variable n is local to the main method. In other words, n doesn't exist inside the someArray method. But since your method has a size parameter, you can use that variable instead.
3. By myList, you really mean someList.
4. You didn't close your someArray method with a curly brace.
5. You're not passing anything in to the someArray method when you call it from the main method. You should be passing in n.
6. You didn't close the main method or the class.

If you need further clarification on any of the above, feel free to ask.

Also, welcome to the ranch!
 
Samuel Norman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome! Now for the update.

I figured out what I was doing wrong,

I figured out to assign the parameter from user input to the method, I had to do this:



So I'm able to reference it into the someArray method.

My next problem, is how do I reference that array that i returned, into more methods?

Say I want to do another method to determine the average of the arrays input. I tried something like this in the main method:

int[] dispArray = displayAve(listArr);

to a method thats titled

Public Static Void displayAve (int[] item).

So if I have all my code right in the displayAve method, how do I get it to reference the numerical values, that were input in a separate method? I tried assigning another value with the ( int[] dispArray = ), and I want to invoke the displayAve method and fill it with the array inputs from the someArray method.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this code may help you --



This is how you can pass the value of the array to some other method.
I hope my answer is helpful, in case of any query do revert back...
 
Joel Christophel
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Samuel Norman wrote:
My next problem, is how do I reference that array that i returned, into more methods?

Say I want to do another method to determine the average of the arrays input. I tried something like this in the main method:

int[] dispArray = displayAve(listArr);

to a method thats titled

Public Static Void displayAve (int[] item).

So if I have all my code right in the displayAve method, how do I get it to reference the numerical values, that were input in a separate method? I tried assigning another value with the ( int[] dispArray = ), and I want to invoke the displayAve method and fill it with the array inputs from the someArray method.



You would use the item reference variable. Something to the effect of:

 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Samuel Norman wrote:
I tried something like this in the main method:

int[] dispArray = displayAve(listArr);

to a method thats titled

Public Static Void displayAve (int[] item).


I assume that is actually

remember - java is case sensitive. "Public" is NOT the same thing as "public".

in any case...if your displayAve (which is a bad name...avenue? avengers? averse?) is declared to be type void, then your dispArray reference variable will be null.
 
    Bookmark Topic Watch Topic
  • New Topic