Click here to Skip to main content
Rate this: bad
good
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.io.*;
import java.util.*;
public class java1
{
	public void GetPoints()
	{
		Scanner input = new Scanner(System.in);
 
        //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;
        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 22:35pm

1 solution

Rate this: bad
good
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();
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.
  Permalink  
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
   
printArray(test.GetPoints());
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 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Apr 2013
Copyright © CodeProject, 1999-2014
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