Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to input user input for array split by space?


Asked by Duncan Briggs on Dec 05, 2021 FAQ



Learn more java user input for Array - split by space Ask Question Asked6 years, 6 months ago Active6 years, 6 months ago Viewed1k times -1 All,
In respect to this,
So when we do split it convert the string to a Array. scanner . nextLine ().split (“ ”) Now to store a array we need a array thats why there is are (String s []) sqaure brackets.
Moreover, Input () function takes input as string data type that’s why all the inputs ‘10’ ‘20’ ‘30’ are of type string. The split () function with zero argument splits the given string on the basis of space and returns a list of elements that’s why we are having the output [’10’, ’20’, ’30’].
Keeping this in consideration,
How to Take Array Input in Java Java does not provide any direct way to take array input. But we can take array input by using the method of the Scanner class. To take input of an array, we must ask the user about the length of the array.
In fact,
There's a very easy way to read input delineated by spaces: use 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". Then you can convert to an int array like this: