简体   繁体   English

从控制台读取多行并将其存储在Java的数组列表中?

[英]Read multiple lines from console and store it in array list in Java?

Can anyone please help me with the code as how to read multiple lines from console and store it in array list? 任何人都可以帮我提供代码,如何从控制台读取多行并将其存储在数组列表中? Example, my input from the console is: 例如,我从控制台输入的是:

12     abc      place1
13     xyz      place2

and I need this data in ArrayList. 我在ArrayList中需要这些数据。

So far I tried this code: 到目前为止我尝试了这段代码:

Scanner scanner = new Scanner(System.in);
ArrayList informationList = new ArrayList<ArrayList>();
String information = "";
int blockSize = 0, count = 1;
System.out.println("Enter block size");
blockSize = scanner.nextInt();
System.out.println("Enter the Information ");
while (scanner.hasNext() && blockSize >= count) {
    scanner.useDelimiter("\t");
    information = scanner.nextLine();
    informationList.add(information);
    count++;
}

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Input line from console is mix of string and integer 控制台的输入行是字符串和整数的混合

You've got a few problems. 你有一些问题。

First of all, the initialization line for your ArrayList is wrong. 首先,ArrayList的初始化行是错误的。 If you want a list of Object so you can hold both Integers and Strings, you need to put Object inside the angle braces. 如果你想要一个Object列表,这样你可以同时拥有整数和字符串,你需要将Object放在角括号内。 Also, you're best off adding the generic type argument to the variable definition instead of just on the object instantiation. 此外,您最好将泛型类型参数添加到变量定义中,而不仅仅是在对象实例化上。

Next, your count is getting messed up because you're initializing it to 1 instead of 0. I'm assuming "block size" really means the number of rows here. 接下来,你的计数搞砸了,因为你把它初始化为1而不是0.我假设“块大小”实际上意味着这里的行数。 If that's wrong leave a comment. 如果那是错的,请发表评论。

Next, you don't want to reset the delimiter your Scanner is using, and you certainly don't want to do it inside your loop. 接下来,您不希望重置扫描程序正在使用的分隔符,并且您当然不希望在循环中执行此分隔符。 By default a Scanner will break up tokens based on any whitespace which I think is what you want since your data is delimited both by tabs and newlines. 默认情况下,扫描程序会根据我认为您想要的任何空格来分解令牌,因为您的数据是由制表符和换行符分隔的。

Also, you don't need to check hasNext() in your while condition. 此外,您不需要在while条件下检查hasNext()。 All of the next*() methods will block waiting for input so the call to hasNext() is unnecessary. 所有下一个*()方法都将阻塞等待输入,因此不需要调用hasNext()。

Finally, you're not really leveraging the Scanner to do what it does best which is parse tokens into whatever type you want. 最后,你并没有真正利用Scanner来做它最擅长的事情,即解析令牌到你想要的任何类型。 I'm assuming here that every data line is going to start with a single integer and the be followed by two strings. 我假设每个数据行都以一个整数开头,然后是两个字符串。 If that's the case, just make a call to nextInt() followed by two calls to next() inside your loop and you'll get all the data parsed out into the data types you need automatically. 如果是这种情况,只需调用nextInt(),然后在循环中调用next(),然后将所有数据解析为自动需要的数据类型。

To summarize, here is your code updated with all my suggestions as well as some other bits to get it to run: 总而言之,这里是您的代码更新了我的所有建议以及一些其他位来运行它:

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

public class Example {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Object> list = new ArrayList<>();
        System.out.println("Enter block size");
        int blockSize = scanner.nextInt();
        System.out.println("Enter data rows:");
        int count = 0;
        while (count < blockSize) {
            list.add(scanner.nextInt());
            list.add(scanner.next());
            list.add(scanner.next());
            count++;
        }
        System.out.println("\nThe data you entered is:");
        System.out.println(list);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM