Java 如何采用新线分隔的输入

Java 如何采用新线分隔的输入,java,arrays,string,Java,Arrays,String,我有一个带换行符的输入。我想把这个字符串转换成一个数组,对于每一行,在数组中跳转一个索引位置 如果输入为: 100 200 三百 然后我需要输出为: 110 210 310 我使用了以下代码: public class NewClass { public static void main(String args[]) throws IOException { BufferedReader br= new BufferedReader(new InputStreamRead

我有一个带换行符的输入。我想把这个字符串转换成一个数组,对于每一行,在数组中跳转一个索引位置

如果输入为:

100
200
三百

然后我需要输出为:

110
210
310

我使用了以下代码:

public class NewClass {
    public static void main(String args[]) throws IOException {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        String str=br.readLine();
        String [] arrOfStr = str.split("\n");int b=0;
        for (String a : arrOfStr) {
            b=Integer.parseInt(a);
            int c=b+10;
            System.out.println(c);
        }
     }
}
但它并没有给出期望的输出。
注意:我们不是从文本文件中获取输入。我们不知道测试用例中有多少输入。我们可以一次性获取输入。

您可以使用
扫描仪。hasNext()
代替-

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    // scanner.useDelimiter("\\r\\n"); // if required to delimit the input 
    while (scanner.hasNext()) {
        int c = Integer.parseInt(scanner.next()) + 10;
        System.out.println(c);
    }
}
使用与您相同的输入-

输出为-


请详细说明我的评论

    List<Integer> intList = new ArrayList<Integer>();
    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    while (!str.isEmpty()) {
        intList.add(Integer.parseInt(str)+10);
        str=br.readLine();

    }
    System.out.println(intList);}
List intList=new ArrayList();
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
字符串str=br.readLine();
而(!str.isEmpty()){
add(Integer.parseInt(str)+10);
str=br.readLine();
}
System.out.println(intList);}

这应该行得通,我正在打电话……

你的代码到底出了什么问题?你试过调试它吗?@dbf是的,但它一次只接受一个输入。使用str.split(“\r\n”)这里有一个完整的线程:@Waffles我只得到110。在outputOh不是210和310我误解了你的问题。。。br.readLine()只读取一行。如果您想在换行后读取它,就必须实现while循环。str.split不适用于这种情况。请参见此处:
readLine()
将在文件末尾返回
null
,而不是空字符串。
110
210
310
    List<Integer> intList = new ArrayList<Integer>();
    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    while (!str.isEmpty()) {
        intList.add(Integer.parseInt(str)+10);
        str=br.readLine();

    }
    System.out.println(intList);}