0

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

0

4 Answers 4

7

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());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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


}

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.