Java Arraylist to store user input

若如初见. 提交于 2019-11-27 08:48:30

问题


Hi I am new to arraylists and java and I was wondering if someone could help me or give me pointers on how to create a program that allows the user to repeatedly enter directory entries from the keyboard and store them in an arraylist.

enter name:
enter telephone number:

and then ask if the user wants to enter another one

enter another:  Y/N

thanks


回答1:


You can still use two ArrayLists, or make a class with name and phone attributes and then make one ArrayList of objects of that class.

First approach shown here.

import java.util.ArrayList;
import java.util.Scanner;

public class AAA {

    public static void main(String[] args) {
        ArrayList<String> name = new ArrayList<String>();
        ArrayList<Integer> phone = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Please enter your name: ");
            name.add(sc.next());
            System.out.println("Please enter your number: ");
            phone.add(sc.nextInt());
        }
    }
}



回答2:


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Tester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        List<String> directoryNames= new ArrayList<String>();


        String input=getDirectoryName();

        String directoryPath="";
        String userChoice="";

        String[] inputTokens=input.split(" ");

        if(inputTokens.length>1)
        {
             directoryPath=inputTokens[0];
             userChoice=inputTokens[1];
        }
        else
        {
            directoryPath=inputTokens[0];
        }

        while(!"q".equalsIgnoreCase(userChoice))
        {
            directoryNames.add(directoryPath);

            input=getDirectoryName();

            inputTokens=input.split(" ");

            if(inputTokens.length>1)
            {
                 directoryPath=inputTokens[0];
                 userChoice=inputTokens[1];
            }
            else
            {
                directoryPath=inputTokens[0];
            }

        }

    }

    public static String getDirectoryName()
    {
        String input="";

        System.out.println("Please Enter Directory name . If you want to quit press q or Q at the end of directory name \n ");
        System.out.println("\n Example <directory_path> q");

        Scanner in = new Scanner(System.in);

        input=in.nextLine().trim();

        return input;
    }


}



回答3:


It seems that you want to use a Map instead of an array list. You want to use the .put(k,v) method to store your inputs.

Map newMap= new Map();

newmap.put(inputName,inputNum);

Link to Map API




回答4:


import java.util.*;

class simple
{
  public static void main(String args[])
  {
    ArrayList<String> al=new ArrayList<String>();
    ArrayList<Integer> al1=new ArrayList<Integer>();
    Scanner ac=new Scanner(System.in);
    al.add(ac.next());
    al1.add(ac.nextInt());
    Iterator itr=al.iterator();
    Iterator itr1=al1.iterator();
    while(itr.hasNext()&& itr1.hasNext())
    {
        System.out.println(itr.next());
        System.out.println(itr1.next());
    }
  }
}


来源:https://stackoverflow.com/questions/4455873/java-arraylist-to-store-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!