Java ArrayList, taking user input of multiple types(int, String etc.) in one line

帅比萌擦擦* 提交于 2019-12-03 16:56:44

try this simple example to print the arraylist values

     import java.util.*;
        class SimpleArrayList{
            public static void main(String args[]){
                List l=new ArrayList();
                System.out.println("Enter the input");
                Scanner input=new Scanner(System.in);

                 String a =input.nextLine();
                 l.add(a);
       // use this to iterate the value inside the arraylist.
      /* for (int i = 0; i < l.size(); i++) {
          System.out.println(l.get(i));
              } */
                    System.out.println(l);

            }

        }

As I think there are enough answers on how to read data from System.in, I'll take a different approach here. First you should be aware that this is not the major way of getting data in java. In fact in more than 10 years I never used it. That's why there's no complete ready to use solution for give me the stuctured data into some container (like ArrayList). Instead you get simply one string per line. And you have to deal with that on your own. this process is called parsing. Depending on the complexity of the chosen syntax there are several approaches like using a parser generator if it's more complex or write the parser by hand in simpler case. I'd like to get into your first suggestion and describe it as comma separated with optional whitespace. For a syntax like this the class Scanner delivers quite some support. Numbers can be recognized and the tokenizing is done almost automatic. However, if you have more specific data you might need some aditional effort, like I demonstrated with a map of animals I used to convert that very special data type. To be flexible enough to solve all the real world problems there can't be a ready to use solution. Only comprehensive support to build your own.

  Map<String, Animal> animals = ...
  Scanner scanner = new Scanner("1, 2, 4, 257, dog, rabbit, 7, #").useDelimiter(",");
  while (scanner.hasNext()) {
    if (scanner.hasNextInt()) {
      result.add(scanner.nextInt());
    } else {
      String val = scanner.next();
      if (animals.containsKey(val)) {
        result.add(animals.get(val));
      } else {
        result.add(val);
      }
    }
  }

One approach is to tokenize the input and then add it into an array like this:


    Scanner scn = new Scanner(System.in);
    System.out.println("Put in a set: ");
    String input = scn.nextLine();
    System.out.println(input);
    Scanner tokenizer = new Scanner(input);
    tokenizer.useDelimiter(" ");
    ArrayList<Object> arr = new ArrayList<Object>();
    while(tokenizer.hasNext())
    {
        arr.add(tokenizer.next());
        System.out.println(arr.get(arr.size()-1));
    }
    System.out.println(arr);
Rakesh Divedi

you can try this code for taking input dinamically in arraylist and store in arraylist

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

public class HelloWorld
{

 public static void main(String []args){
     Scanner sc=new Scanner(System.in);
     String j;
     ArrayList l=new ArrayList();
     for(int i=0;i<6;i++)
     {  j=sc.nextLine();
         l.add(j);
     }
    System.out.println("Hello World"+l);
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!