Java Arraylist to store user input

你说的曾经没有我的故事 提交于 2019-11-28 14:42:16

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


}
Chris

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

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