We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Strings
- Java String Tokens
- Discussions
Java String Tokens
Java String Tokens
Sort by
recency
|
1733 Discussions
|
Please Login in order to post a comment
import java.util.*;
public class Solution {
String s = scan.nextLine(); // s = s.trim();
// Split by commas, question marks, spaces, and apostrophes String[] s1 = s.split("[^a-zA-Z0-9]+");
int nonEmptyCount = 0; for (String word : s1) { if (!word.isEmpty()) { nonEmptyCount++; } }
// Print the word count first System.out.println(nonEmptyCount);
// Print each non-empty word for (String word : s1) { if (!word.isEmpty()) { System.out.println(word); } }
This Worked for me. scan.close();
} }
import java.io.; import java.util.;
public class Solution {
}
public class Solution {
}
import java.io.; import java.util.; /* *@Author : Rehan *Using Stream API in Java8 */ public class SolutionDump {
}
Why it needs to trim()?
If it doesn't trim() , it will not pass all the test even if they output the same thing
Why?
I would like to know