在java中读取由空格分隔的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27880245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:13:04  来源:igfitidea点击:

reading input delimited by spaces in java

javainputstreamjava.util.scanner

提问by Dev

im struggling with the below question.

我正在努力解决以下问题。

Question: Input contains the number of blocks n (1 ≤ n ≤ 20) and weights of the blocks w1, …, wn (integers, 1 ≤ wi ≤ 100000) delimited by white spaces. the input should be taken from the user.

问题:输入包含块数 n (1 ≤ n ≤ 20) 和块的权重 w1, …, wn (整数, 1 ≤ wi ≤ 100000) 由空格分隔。输入应该来自用户。

  1. how should i write the input with spaces? (lets say my input for no. of blocks is n =5 and the weights of the blocks be : 2 3 55 6 33 - because n=5)
  2. how should i read the input which are given with white spaces and store it in some list or array ? using what commands.
  1. 我应该如何用空格写输入?(假设我输入的块数是 n = 5,块的权重是:2 3 55 6 33 - 因为 n=5)
  2. 我应该如何读取带有空格的输入并将其存储在某个列表或数组中?使用什么命令。

please find my code:

请找到我的代码:

public static void main(String[] args) {
    int b1 = 0;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter no. of blocks: ");
    b1 = in.nextInt();
    if (b1<=20) {
        in.nextLine();
        int[] arr = new int[b1];
        for (int i=0; i<b1; i++) {
            System.out.println("Enter a weights of ths blocks: ");
            if (arr[i]<=100000) {
          arr[i] = in.nextInt();




            }
    }
        }

i dont think this is the right way coz the input should be delimited with spaces..

我不认为这是正确的方法因为输入应该用空格分隔..

i was thinking about the ways to proceed but couldnt come up with any solution. can u please help me our on this guys. thanks.

我正在考虑继续的方法,但无法想出任何解决方案。你能帮我解决这个问题吗?谢谢。

采纳答案by Mithun

  1. The input can be given at command line as "input1 input2 input3" (Weights separated by spaces)

  2. You can use Scanner to read the input. Consider the sample code below:

  1. 输入可以在命令行中给出为“input1 input2 input3”(权重用空格分隔)

  2. 您可以使用 Scanner 读取输入。考虑下面的示例代码:

Assuming the input is only integers.

假设输入只是整数。

 Scanner scanner = new Scanner(System.in);
 int numOfBlocks = scanner.nextInt();
 int weightArray[] = new weightArray[numOfBlocks];
 for(int i=0;i<numOfBlocks;i++)
       {
        weightArray[i] = scanner.nextInt();
       }
 scanner.close();
//your logic

回答by Alex K

There's a very easy way to read input delineated by spaces: use string.split(delimit):

有一种非常简单的方法来读取由空格划定的输入:使用string.split(delimit)

String input = "this is a test";
String[] tokens = input.split(" ");

That'll give you an array, tokens, of "this", "is", "a", and "test".

这会给你一个数组,tokens,“this”、“is”、“a”和“test”。

Then you can convert to an int array like this:

然后你可以像这样转换成一个 int 数组:

int[] inputNumbers = new int[tokens.length];
for(int i = 0; i < tokens.length; i++) {
    inputNumbers[i] = Integer.parseInt(tokens[i]);
}

Just make sure that you are sure that you have numbers as an input.

只要确保你确定你有数字作为输入。

回答by ProgrammingIsAwsome

Here is my solution: The user enters first the number of weights of the block and then presses enter. Afterwards he enters all the weigths of the block in one line seperated by spaces (4 2 3 1) and then presses enter. The check if the single values are in correct range, you have to do by yourself.

这是我的解决方案:用户首先输入块的权重数,然后按 Enter。之后,他在一行中输入块的所有重量,以空格 (4 2 3 1) 分隔,然后按 Enter。检查单个值是否在正确的范围内,您必须自己完成。

public static void main(String[] args) {
    int b1 = 0;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter no. of blocks: ");
    b1 = Integer.parseInt(in.nextLine());
    int[] arr = new int[b1];
    if ((b1 <= 20) && (1 >= 0)) {
        System.out.println("Enter all weights of the block seperated by spaces and then press enter:");
        String readLine = in.nextLine();
        String[] weights = readLine.split(" ");
        for (int i = 0; i < b1; ++i) {
            arr[i] = Integer.parseInt(weights[i]);
        }
    }
    in.close(); //don't forget to close the resource!
}