Java - How to read integers separated by a space into an array

微笑、不失礼 提交于 2019-12-18 11:43:06

问题


I am having trouble with my project because I can't get the beginning correct, which is to read a line of integers separated by a space from the user and place the values into an array.

    System.out.println("Enter the elements separated by spaces: ");
    String input = sc.next();
    StringTokenizer strToken = new StringTokenizer(input);
    int count = strToken.countTokens();
    //Reads in the numbers to the array
    System.out.println("Count: " + count);
    int[] arr = new int[count];

    for(int x = 0;x < count;x++){
        arr[x] = Integer.parseInt((String)strToken.nextElement());
    }

This is what I have, and it only seems to read the first element in the array because when count is initialized, it is set to 1 for some reason.

Can anyone help me? Would it be better to do this a different way?


回答1:


There is only a tiny change necessary to make your code work. The error is in this line:

String input = sc.next();

As pointed out in my comment under the question, it only reads the next token of input. See the documentation.

If you replace it with

String input = sc.nextLine();

it will do what you want it to do, because nextLine() consumes the whole line of input.




回答2:


String integers = "54 65 74";
List<Integer> list = new ArrayList<Integer>();
for (String s : integers.split("\\s"))  
{  
   list.add(Integer.parseInt(s));  
}
list.toArray();



回答3:


There are alternate ways to achieve the same. but when i tried your code, it seems to work properly.

StringTokenizer strToken = new StringTokenizer("a b c");
int count = strToken.countTokens();
System.out.println(count);

It prints count as 3. default demiliter is " "

I dont know how are you getting your input field. May be it is not returning the complete input in string format.

I think you are using java.util.Scanner for reading your input

java doc from scanner.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

Hence the input is returning just one Integer and leaving the rest unattended

Read this. Scanner#next(), You should use Scanner#nextLine() instead




回答4:


This would be a easier way to do the same -

System.out.println("Enter the elements seperated by spaces: ");
String input = sc.nextLine();
String[] split = input.split("\\s+");
int[] desiredOP = new int[split.length];
int i=0;
for (String string : split) {
    desiredOP[i++] = Integer.parseInt(string);
}



回答5:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
.
.
.
StringTokenizer st = new StringTokenizer(br.readLine());
int K = Integer.parseInt(st.nextToken());
int N= Integer.parseInt(st.nextToken());


来源:https://stackoverflow.com/questions/14333681/java-how-to-read-integers-separated-by-a-space-into-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!