Umesh Pathak Umesh Pathak - 3 days ago 6
Java Question

return array from one method to another method

This is my program and every time it is asking me to input the array.
I want to input a array once and process on that array.
But this program is asking me to input array again.

from method named "first" I just want to return array and use that array in two different methods add and delete. But it is always asking me to input all the elements of array that is every time the first method is running while i call any add or delete method from main method.

package Program;

import java.util.Arrays;
import java.util.Scanner;

public class Functionality {
public static int[] first( )
{
System.out.println("Enter the number of element in array");
Scanner num = new Scanner(System.in);
int data = num.nextInt();
//return data;
Scanner ar = new Scanner(System.in);
int arr[] = new int[data];
System.out.println("Enter "+data+" Numbers");
for(int i =0; i<data; i++){
System.out.println("Enter NUmber :"+(i+1));
arr[i] = num.nextInt();
}
System.out.println();
System.out.println(Arrays.toString(arr));
return arr;
}

public static void main(String[] args) {
add();
delete();

}
static void add(){
int arr[]=first();
System.out.println("Enter the number you want to add");
Scanner one = new Scanner(System.in);
int naya = one.nextInt();
for(int i = 0; i<=arr.length-1; i++){
arr[i]= arr[i] + naya;
}
System.out.println("The added array is");
System.out.println(Arrays.toString(arr));
}
static void delete(){
int arr[]=first();
System.out.println("Enter the number you want to substract");
Scanner two = new Scanner(System.in);
int arko = two.nextInt();
for(int i =0; i <= arr.length-1; i++ ){
arr[i]=arr[i]-arko;
}
System.out.println("The Substracted array is");
System.out.println(Arrays.toString(arr));

}

Answer

You should obtain reference to an array and passing it during subsequent methods invocations.

This should do the trick:

class Functionality {

static int[] first() {
    System.out.println("Enter the number of element in array");
    Scanner num = new Scanner(System.in);
    int data = num.nextInt();
    //return data;
    Scanner ar = new Scanner(System.in);
    int arr[] = new int[data];
    System.out.println("Enter " + data + " Numbers");
    for (int i = 0; i < data; i++) {
        System.out.println("Enter NUmber :" + (i + 1));
        arr[i] = num.nextInt();
    }
    System.out.println();
    System.out.println(Arrays.toString(arr));
    return arr;
}

public static void main(String[] args) {
    int arr[] = first();
    add(arr);
    delete(arr);

}

static void add(int arr[]) {
    System.out.println("Enter the number you want to add");
    Scanner one = new Scanner(System.in);
    int naya = one.nextInt();
    for (int i = 0; i <= arr.length - 1; i++) {
        arr[i] = arr[i] + naya;
    }
    System.out.println("The added array is");
    System.out.println(Arrays.toString(arr));
}

static void delete(int arr[]) {
    System.out.println("Enter the number you want to substract");
    Scanner two = new Scanner(System.in);
    int arko = two.nextInt();
    for (int i = 0; i <= arr.length - 1; i++) {
        arr[i] = arr[i] - arko;
    }
    System.out.println("The Substracted array is");
    System.out.println(Arrays.toString(arr));

}

}

Your program is asking You every time to input array, because every time You invoke method first() new array is created

Comments