watacus
watacus

Reputation: 21

Pass ArrayLists from one class to another

I'm relatively new to Java, so apologies if I've made any obvious mistakes. I'm trying to pass the ArrayList "numbers" from the class "IO" to the class "Interaction". I'm attempting to print the contents of numbers to the console as a test. When I call the method getNumbers, the console remains empty. What am I doing wrong? Here's what I have:

IO Class:

import java.io.*;
import java.util.*;

public class IO {
    public static void main (String[] args){
        fileInput();
    }
    public static void fileInput() {
        //variable setup
        String file = "src/seats.txt";
        int number;

        ArrayList<Integer> numbers = new ArrayList<Integer>();


        //Read file into the ArrayLists
        try {
            Scanner input = new Scanner(new FileReader(file));

            while (input.hasNextLine()){    //read a line from file
                number = input.nextInt();
                input.nextLine();

                numbers.add(number);
            }
            input.close();
        }
        catch (IOException e){
            System.out.println(e);
        }
        public static ArrayList<Integer> getNumbers(){
            ArrayList<Integer> numbers = new ArrayList<Integer>();
            return numbers;
        }
    }

Interaction class:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Interaction {

    public static void main(String[] args) {
        menu();
        IO.fileInput();
    }
    static Scanner consoleInput = new Scanner (System.in);
    public static void menu (){
        String userChoice = "";

        while (!userChoice.equals("QUIT")){
            System.out.println("|            MAIN MENU              |");
            System.out.println("|-----------------------------------|");
            System.out.println("|                                   |");
            System.out.println("| 1.) Reserve a Seat                |");
            System.out.println("| 2.) Cancel a Reservation          |");
            System.out.println("| 3.) View Waiting List             |");
            System.out.println("| Quit.) Exit Program               |");
            System.out.println("|                              v0.1 |");
            System.out.println("\nPlease make your selection:");

            userChoice = consoleInput.next().toUpperCase();

            switch(userChoice){
                default:
                if (!userChoice.equals("1") && !userChoice.equals("2") && !userChoice.equals("3")){
                System.out.println("Invalid option, please try again.");
            }
            pause();
            break;
            case "1":
            if (userChoice.equals("1")){
                reserveSeat();
            }
            pause();
            break;
            case "2":
            if(userChoice.equals("2")){
                getNumbers();
                cancel();
            }
            pause();
            break;
            case "3":
            if(userChoice.equals("3")){
                waitingList();
            }
            pause();
            break;
            case "QUIT":
            System.out.println("Thank you for using our service!");
            consoleInput.close();
            }
        }
     }

    private static void reserveSeat() {
        String type, direction, userChoice = null;
        int number;

    private static void cancel() {
        System.out.println("Test 2");
    }
    private static void waitingList() {
        System.out.println("Test 3");
    }
    private static void pause(){
        System.out.println("Press enter to return to the menu.");
        Scanner enter = new Scanner(System.in);
        enter.nextLine();
    }
    public static void getNumbers(){
        ArrayList<Integer> numbers = IO.getNumbers();
        System.out.println();
        int sz = numbers.size();
        for (int i = 0; i < sz; i++){
            System.out.println(numbers.get(i).toString());
        }
    }
}

I'm aware similar questions have been asked before, but I couldn't find anything particularly helpful. Any help would be greatly appreciated.

Edit: now shows entire Interaction class, not just getNumbers()

Upvotes: 1

Views: 1187

Answers (3)

iblancasa
iblancasa

Reputation: 364

I think your code has some problems:

Here, you are creating a new ArrayList and returning it (empty). So, when you try to print it... there are no elements to show.

public static ArrayList<Integer> getNumbers(){
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    return numbers;
}

You need to define your number ArrayList out the "fileInput" method. Then, you need to work with it in "fileInput" and return it (or a copy of it, you decide) in "getNumbers" method.

Then, when you call "IO.getNumbers()" from Interaction class, you should call "IO.fileInput()" before.

I hope my answer will be helpful for you.

Good luck!

Upvotes: 0

Imesha Sudasingha
Imesha Sudasingha

Reputation: 3570

You have done a severe mistake here. You have called a static method IO.getNumbers() which initializes an empty ArrayList and return it. Since you have forgotten to get the actual ArrayList from fileInput() method, you can use the following code in the IO class.

public class IO {
public static void main (String[] args){
    fileInput();
}
public static ArrayList<Integer> fileInput() {
    //variable setup
    String file = "src/seats.txt";
    int number;

    ArrayList<Integer> numbers = new ArrayList<Integer>();


    //Read file into the ArrayLists
    try {
        Scanner input = new Scanner(new FileReader(file));

        while (input.hasNextLine()){    //read a line from file
            number = input.nextInt();
            input.nextLine();

            numbers.add(number);
        }
        input.close();
    }
    catch (IOException e){
        System.out.println(e);
    }
    return numbers;
}
}

You can modify the getNumbers method as follows then.

public static void getNumbers(){
ArrayList<Integer> numbers = IO.fileInput();
System.out.println();
int sz = numbers.size();
for (int i = 0; i < sz; i++){
    System.out.println(numbers.get(i).toString());
}

Edit: Please note that you must give a meaningful name to your static method other than fileInput(), because it is returning an ArrayList of Integers.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

You want the same numbers to be visible in IO.fileInput() and IO.getNumbers(); you could add a class (static) numbers field, and use it in both places (and program to the List interface). Something like

private static List<Integer> numbers = new ArrayList<>();
public static List<Integer> getNumbers(){
    return numbers;
}

Then, don't forget to remove the numbers declaration from fileInput (so that it uses the static field).

Upvotes: 1

Related Questions