Click here to Skip to main content
11,812,712 members (73,116 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Java
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.util.*;
public class java1
	public void GetPoints()
		Scanner input = new Scanner(;
        //allow user  input;
        System.out.println("Please enter n points");
		int num = input.nextInt();
		int array[] = new int[num];
		for (int i = 0 ; i < array.length; i++ )
           array[i] = input.nextInt();
		//return  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();
Posted 31-Mar-13 21:35pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

So just save the value that the GetPoints methoid returns, aand call printArray from your main method:
int array[] = test.GetPoints();
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 at 1-Apr-13 3:43am
Thanks for the quick answer.
OriginalGriff at 1-Apr-13 3:50am
You're welcome!
nischalinn at 1-Apr-13 5:26am
hello OrignialGriff: how could I get the return type of GetPoints() directly to printArray method??
OriginalGriff at 1-Apr-13 5:29am
should do it.
nischalinn at 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 at 1-Apr-13 5:54am
No! :laugh:
It's your homework, not mine!
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 at 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 at 1-Apr-13 6:05am
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Abhinav S 205
1 CHill60 180
2 KrunalRohit 169
3 Palash Mondal_ 140
0 OriginalGriff 3,877
1 KrunalRohit 2,496
2 CPallini 2,240
3 Maciej Los 2,205
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100