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

查看:25
本文介绍了从控制台读取多行并将其存储在 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

我需要 ArrayList 中的这些数据.

and I need this data in 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++;
}

非常感谢任何帮助.

来自控制台的输入行是字符串和整数的混合

Input line from console is mix of string and integer

推荐答案

您遇到了一些问题.

首先,你的 ArrayList 的初始化行是错误的.如果您想要一个 Object 列表以便您可以同时保存整数和字符串,则需要将 Object 放在尖括号内.此外,最好将泛型类型参数添加到变量定义中,而不仅仅是在对象实例化中.

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.

此外,您不需要在 while 条件中检查 hasNext().所有 next*() 方法都会阻塞等待输入,因此不需要调用 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.

总而言之,这里是您的代码,根据我的所有建议以及其他一些内容进行了更新以使其运行:

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天全站免登陆