从控制台读取多行并将其存放在数组列表中的Java? [英] Read multiple lines from console and store it in array list in Java?

查看:268
本文介绍了从控制台读取多行并将其存放在数组列表中的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以请帮我用code,如何从控制台读取多个行并将其存放在数组列表?
例如,从控制台我的输入是:

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

和我需要的ArrayList此数据。

and I need this data in ArrayList.

到目前为止,我尝试这样做code:

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

任何帮助是极大AP preciated。

Any help is greatly appreciated.

从控制台输入线串和整数混合

Input line from console is mix of string and integer

推荐答案

您已经有了一些问题。

首先,为您的ArrayList初始化线是错误的。如果你想对象的列表,这样你就可以同时按住整数和字符串,你需要把对象角括号内。另外,你最好将泛型类型参数变量定义,而不是仅仅对对象实例化。

First of all, the initialization line for your ArrayList is wrong. If you want a list of Object so you can hold both Integers and Strings, you need to put Object inside the angle braces. Also, you're best off adding the generic type argument to the variable definition instead of just on the object instantiation.

接下来,您计数得到搞砸了,因为你把它初始化为1而不是0,我假设块大小的真正含义的行数在这里。如果说的不对发表评论。

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. 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.

此外,你不需要检查规则hasNext()在while条件。所有的下一个*()方法将阻塞等待输入,因此调用规则hasNext()是不必要的。

Also, you don't need to check hasNext() in your while condition. All of the next*() methods will block waiting for input so the call to hasNext() is unnecessary.

最后,你没有真正利用扫描仪做自己最擅长的是分析令牌,任何你想要的类型。我假定这里,每一个数据线将开始与单个整数和后跟两个字符串。如果是这样的情况下,只需做出nextInt()的调用后跟两个调用next()的循环内,你会得到解析出到你需要自动数据类型中的所有数据。

Finally, you're not really leveraging the Scanner to do what it does best which is parse tokens into whatever type you want. 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.

总之,这里是我的所有建议,以及一些其他位更新您的code让它运行:

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

这篇关于从控制台读取多行并将其存放在数组列表中的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆