12,997,210 members (76,972 online)
Rate this:
See more:
I am learning JAVA.

I have created an array inside a method and now I have to return(as return type ARRAY) that array to main method and another user-defined method and print the values of that array from the main method and the user-defined method accroding to requirement.

Till now I have completed to print the values of the array from another method.

Please have a look at my code.

How can I do that?

```import java.io.*;
import java.util.*;
public class java1
{
public void GetPoints()
{
Scanner input = new Scanner(System.in);

//allow user  input;
int num = input.nextInt();

int array[] = new int[num];

for (int i = 0 ; i < array.length; i++ )
{
array[i] = input.nextInt();
}
//return  array;
printArray(array);
}

public void printArray(int arr[])
{

int n = arr.length;
System.out.println("These are the numbers you have entered.");

for (int i = 0; i < n;i=i+2) {
System.out.print(arr[i] + " ");
System.out.print("(X:"+arr[i] + " "+",Y:"+arr[i+1] + " "+")");
}
}
public static void main(String args[])
{
java1 test = new java1();
test.GetPoints();
}
}```
Posted 31-Mar-13 21:35pm

Rate this:

## Solution 1

So just save the value that the GetPoints methoid returns, aand call printArray from your main method:
```int array[] = test.GetPoints();
printArray(array);```
All you have to do is set the return type for the GetPoints method to an array of ints - at the moment it is void (i.e. it returns nothing) and return the array as in your comments.
nischalinn 1-Apr-13 3:43am

OriginalGriff 1-Apr-13 3:50am

You're welcome!
nischalinn 1-Apr-13 5:26am

hello OrignialGriff: how could I get the return type of GetPoints() directly to printArray method??
OriginalGriff 1-Apr-13 5:29am

printArray(test.GetPoints());
should do it.
nischalinn 1-Apr-13 5:53am

Thanks for the solution.
Could you please provide me link for defining user-defined multi-dimensional arrays and asking for user input for the same.
Thank You!!!
OriginalGriff 1-Apr-13 5:54am

No! :laugh:
I think you should do at least part of it, don't you?
If you get completely stuck, then ask again, showing where you are so far.
But you won't learn if I just give you all the answers, will you? :)
nischalinn 1-Apr-13 6:00am

Ok I'll do it and share with you what I have done and where I have stuck.
Thank You!!!
OriginalGriff 1-Apr-13 6:05am

You're welcome!

Top Experts
Last 24hrsThis month
 ppolymorphe 329 RickZeeland 265 OriginalGriff 260 Karthik Bangalore 130 F-ES Sitecore 130
 OriginalGriff 4,442 ppolymorphe 2,242 Kornfeld Eliyahu Peter 1,973 RickZeeland 1,901 Richard Deeming 1,543