【问题标题】:Taking any number of space separed inputs till a new line取任意数量的空格分隔输入直到换行
【发布时间】:2015-07-25 12:49:52
【问题描述】:

我应该如何将未知数量的空格分隔整数作为输入,直到用户在 java 中按下 enter 并将每行的输入存储在单独的数组中。

例如:如果我有以下几行作为输入,

1 2 3 4 5

2 4 6 8 10

然后一个数组存储第一行(比如)arr1={1,2,3,4,5} 并且下一个数组存储第二行(例如) arr2={2,4,6,8,10}

我尝试使用扫描器类来获取空格分隔的整数输入,但遇到新行时无法切换到下一个数组。

【问题讨论】:

  • 发布您的代码,以便我们调试。

标签: java input


【解决方案1】:

我会这样做:

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

public class Main {

    public static void main(String[] args) {
        List<Integer[]> l = new ArrayList<Integer[]>();
        Scanner s = new Scanner(System.in);
        System.out.println("How many rows will you enter?");
        int numOfRows = s.nextInt();
        for (int i = 0; i < numOfRows; i++) {
            String[] arr = s.nextLine().split(" ");
            Integer[] arr2 = new Integer[arr.length];
            for (int j = 0; j < arr.length; j++) {
                arr2[j] = Integer.parseInt(arr[j]);
            }
            l.add(arr2);
        }
        s.close();
        //Do whatever you want with l
    }
}

【讨论】:

  • 对此进行改进我可能会建议使用 while (s.hasNextLine()) 而不是提前询问用户。话虽如此,我感谢您指出scanner.nextLine,我不熟悉它。
  • 没问题,但他将如何实现,因为我假设他正在从控制台输入读取
  • 一个有效的观点,在这种情况下,我想他将不得不向用户询问该信息。
  • @RoyShahaf 如果此答案对您有所帮助,请考虑对其进行投票并将其标记为正确!祝你有美好的一天!
  • 我要感谢第一个支持者让我超过了 1000 个代表的里程碑!
【解决方案2】:

首先,对于每行未知数量的整数,我建议使用较小的数据结构,也许是一个列表。

其次,如果您希望读取行(使用它的方法 readLine),BufferedReader 似乎是更好的选择。读取一行后,您可以使用 string.split(" ") 获取整数。

【讨论】:

  • 他最好使用扫描仪,就像我的回答一样。
【解决方案3】:

获取输入直到出现空行,然后将该行转换为String 数组,使用空格作为分隔符。然后将每个元素解析为int,并放到一个临时的ArrayList&lt;Integer&gt;ArrayList&lt;Integer&gt; 中的已解析整数将放在主 ArrayList 中。

ArrayList 的结构将是 ArrayListArrayList&lt;Integer&gt;

import java.util.*;

public class MultiLine {
    public static void main (String[] args) {

        Scanner sc = new Scanner( System.in );
        System.out.println("Input:");

        // main ArrayList
        ArrayList < ArrayList<Integer> > lst = new ArrayList < ArrayList<Integer> >();

        // input process
        while ( sc.hasNextLine() ) {

            String line = sc.nextLine();

            // stops input if line is empty
            if ( line.equals("") )
                break;

            // splits line to String array, using space as delimiter
            String[] splittedLine = line.split(" ");

            // temporary ArrayList that will parse each number from lines seperated by space
            ArrayList <Integer> temp = new ArrayList <Integer>();

            // parses each String of splittedLine to int, adds it to temp ArrayList
            for( String num : splittedLine ) {
                try {
                    temp.add( Integer.parseInt(num) );
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            // adds temp ArrayList to lst
            lst.add( temp );
        }
        System.out.println( lst );
    }
}

样本运行

Input:
1 2 3 4 5
2 4 6 8 10
12 14
895 623 1245 1

[[1, 2, 3, 4, 5], [2, 4, 6, 8, 10], [12, 14], [895, 623, 1245, 1]]

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2016-03-12
    • 2015-08-17
    • 2015-06-02
    相关资源
    最近更新 更多