开发者

Java Arraylist to store user input

Hi I开发者_Python百科 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


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());
        }
    }
}


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;
    }


}


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


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());
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜