Home > Enterprise >  Adding console input to ArrayList in Java
Adding console input to ArrayList in Java

Time:11-27

I'm trying to add items from the console input and scanner input to arraylists in java. (To run the program user types Program ID)

The problem is that each time I run the program the contents of the arraylists update to only what has been entered that time. I would like the arraylists to contain all of the inputs that have been entered.

public class User{

private static List<String> listNames = new ArrayList<String>();
private static List<Integer> listIds = new ArrayList<Integer>();

public static void main(String[] args)
    {
        int tempID = 5000;
        if (args.length>0) tempID= Integer.parseInt(args[0]);
        System.out.println("Login  " tempID);
  
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your Name : ");
        tempName = scanner.nextLine();
        
        User n = new User();
        n.ID= tempID;
        n.name = tempName;
        listIds.add(n.ID);
        listNames.add(n.name);

        }
     }


}

Does anyone know if this is possible?

CodePudding user response:

Everytime you compile your programm you ceate new Arrays. So you need like another way of saving your inputs like in a SQL Database or you can run your programm infinitly with a loop and ask each time for a new entry and collect it with the scanner like you already did but with that method you can do noting with the array entries because your asking for entries never stops.

CodePudding user response:

Everytime you run the program it is going to initialize a different array. To solve this, you can store your data in a database such as MySQL or Oracle. But a more efficient way to solve this is that to save your ArrayList as an object locally using java.io.ObjectInputStream and java.io.ObjectOutputStream
I wrote the following 2 functions

public static ArrayList<Object> loadArrayList(String filename)
{
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream(filename));
        ArrayList<Object> arr = (ArrayList<Object>) ois.readObject();
        return arr;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}   
public static void saveArrayList(ArrayList<Object> arr, String filename)

{
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream(filename));
        oos.writeObject(arr);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Call saveArrayList with the first argument is your ArrayList instance and the second argument the filename. You can then load from your local file system using the loadArrayList method with the argument is your filename.
In your case, just call the saveArrayList method when your user exits the program and call the loadArrayList to get all your elements from the previous inputs to resume the user's progress

  • Related