How to split a string with space being the delimiter using Scanner

Zack

I am trying to split the input sentence based on space between the words. It is not working as expected.

public static void main(String[] args) {
    Scanner scaninput=new Scanner(System.in);
    String inputSentence = scaninput.next();
    String[] result=inputSentence.split("-");
    // for(String iter:result) {
    //     System.out.println("iter:"+iter);
    // }
    System.out.println("result.length: "+result.length);
    for (int count=0;count<result.length;count++) {
        System.out.println("==");
        System.out.println(result[count]);
    }
}

It gives the output below when I use "-" in split:

fsfdsfsd-second-third
result.length: 3
==
fsfdsfsd
==
second
==
third

When I replace "-" with space " ", it gives the below output.

first second third
result.length: 1
==
first

Any suggestions as to what is the problem here? I have already referred to the stackoverflow post How to split a String by space, but it does not work.

Using split("\\s+") gives this output:

first second third
result.length: 1
==
first
Bohemian

Change

scanner.next()

To

scanner.nextLine()

From the javadoc

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

Calling next() returns the next word.
Calling nextLine() returns the next line.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to split a string using space delimiter in C unix?

How to split string into rows with space delimiter in Bigquery?

Split String at Last Space before Delimiter Using preg_split?

How to split string using CRLF delimiter in cpp?

How to split a string using a specific delimiter in Arduino?

How to split string with "-" as delimiter

how to split a string with a " " delimiter?

Split string by delimiter by using vectors - how to split by newline?

How can I use regex to split a string, using a string as a delimiter?

Using sed to split a string with a delimiter

Split the content of a string using as delimiter the "="

How to split query string using Multiple Delimiter (=, !=, >=) using regex

using patterns/delimiter to get String from Scanner

How to split a String and keep the delimiter '='

PHP: how to split a string with a delimiter

How to split a string using the period/dot/decimal point '.' as a delimiter in R

How to split a text using a string delimiter with R's strsplit?

How did I split a string using re module with '()' as a delimiter?

How can I split string by not pereating delimiter using regexp?

Split string by space and space as delimiter in Oracle with regexp_substr

split string in substring using a string delimiter

Split Java String into Two String using delimiter

How to split a String by space

How can we split a string by sentence as a delimiter (two words with space between)?

How to split a string column into two column by varying space delimiter on its last occurence

Split the string using string delimiter and display the data along with delimiter

How can I split a string with a string delimiter?

How to split a string by an empty string delimiter

How to split string by space only in middle of string using JS