如何从Java中的单行输入读取多个Integer值? [英] How to read multiple Integer values from a single line of input in Java?

查看:129
本文介绍了如何从Java中的单行输入读取多个Integer值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,我希望允许用户在出现提示时输入多个整数.我尝试使用扫描仪,但发现它仅存储用户输入的第一个整数.例如:

I am working on a program and I want to allow a user to enter multiple integers when prompted. I have tried to use a scanner but I found that it only stores the first integer entered by the user. For example:

输入多个整数:1 3 5

Enter multiple integers: 1 3 5

扫描仪将仅获取第一个整数1.是否可以从一行中获取所有3个不同的整数并在以后使用?这些整数是我需要根据用户输入操作的链表中数据的位置.我无法发布源代码,但我想知道是否可行.

The scanner will only get the first integer 1. Is it possible to get all 3 different integers from one line and be able to use them later? These integers are the positions of data in a linked list I need to manipulate based on the users input. I cannot post my source code, but I wanted to know if this is possible.

推荐答案

我一直在hackerearth上使用它

I use it all the time on hackerearth

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String  lines = br.readLine();    

    String[] strs = lines.trim().split("\\s+");

    for (int i = 0; i < strs.length; i++) {
    a[i] = Integer.parseInt(strs[i]);
    }

这篇关于如何从Java中的单行输入读取多个Integer值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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