Home > Software engineering >  loosing data when exiting a method
loosing data when exiting a method

Time:11-02

I'm self learning programming and practicing my skills as I move forward. I've been consistently encountering a bug, and fixing it by trial and error but haven't really learned my actual mistake so far, if it is syntax or logic and how to avoid encountering it in the future.

my problem is as follows: I build a method that uses certain data or inputs certain data, and after the program successfully runs through the method and exits it, the data that was created/sorted/input within the method is lost for the rest of the program. If anyone would help me understand what I am missing every time, I'd really appreciate it.

public static void EnterArray(double[] array, int arraySize) {
        Scanner input = new Scanner(System.in);
        System.out.println("Array size: ");
        arraySize = input.nextInt();
        array = new double[arraySize];
        for(int i=0; i<arraySize; i  ) {
            array[i]= input.nextInt();
        }
        input.close();
    }

This method will create an array from the users input. Within the method, the data is stored correctly, but once it exits the method, the array created is gone. I would like to use that array within my next steps and methods in my program

Thank you!

CodePudding user response:

You could change your method signature and return the array you build from the method:

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        double[] arrayFromMethod = MyClass.buildArray();
        System.out.println(Arrays.toString(arrayFromMethod));
    }
    
    public static double[] buildArray() {
        Scanner input = new Scanner(System.in);
        System.out.println("Array size: ");
        int arraySize = input.nextInt();
        double[] array = new double[arraySize];
        for(int i=0; i<arraySize; i  ) {
            array[i]= input.nextInt();
        }
        input.close();
        
        return array;
    }
}

CodePudding user response:

If you want the Array to be class global then declare it as a class member (instance) variable, for example:

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

public class MyClass {

    private final Scanner input = new Scanner(System.in);
    double[] array;

    public static void main(String[] args) {
        // Started this way to avoid the need for statics.
        new MyClass().startApp(args);
    }

    private void startApp(String[] args) {
        fillArray();
        
        // Display Array In Console Window...
        System.out.println(Arrays.toString(this.array));
    }

    public void fillArray() {
        int arraySize = 0;
        while (arraySize < 1) {
            System.out.print("Enter an Array size: -> ");
            try {
                arraySize = input.nextInt();
            } catch (Exception ex) {
                System.out.println("Invalid Entry! Try again...");
                arraySize = 0;
                input.nextLine();  // consume ENTER key hit.
            }
        } 
    
        array = new double[arraySize];
        
        for(int i = 0; i < arraySize; i  ) {
            double val = 0d;
            while (val == 0d) {
                val = 0d; 
                System.out.print("Enter Array Element #"   (i 1)   ": -> ");
                try {
                    val = input.nextDouble();
                } catch (Exception ex) {
                    System.out.println("Invalid Entry! Try again..." 
                                         System.lineSeparator());
                    val = 0d;
                    input.nextLine();  // consume ENTER key hit.
                }  
            }
            array[i] = val;
        }
    }
}

And don't use nextInt() to fill a double array, use nextDouble(). Also don't close the System.in stream unless you know for sure your application wont need it any more. If you do, you will need to restart the application in order to get User input again. The JVM will close the System.in Stream when the application closes.

  • Related