Is there a way to create some type of For loop to separate a string with spaces?
So far I can display a string and find how many characters it has.
import java.util.Scanner;
import java.lang.String;
public class Word{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int b;
String word;
System.out.println ("Enter a word: ");
word = scan.next();
b = word.length();
System.out.println (word);
System.out.println (b);
}
}
As an alternative to Scanner, you can do something like the following:
String[] parts = line.split(" ");
for (String part : parts) {
//do something interesting here
}
Use the split() method in the String class, like this:
String line = "a series of words";
String[] words = line.split("\\s+");
It will return a String[] with the individual words in line, for the above example it will produce this:
{"a", "series", "of", "words"}
You can use String.split to split the sentence into an array of words.. something like
string[] words = word.split(" ");
for (String s: words)
{
System.out.println(s);
System.out.println("\n");
}
have you tried looking at the java API?
there are a lot of different functions you can use ...
top of my head split would work nicely and you wouldn't even have to write a loop
http://docs.oracle.com/javase/6/docs/api/
This is untested, but I feel like this is a learning exercise so this should put you on the right path. In the for loop you access each character of the word as a sub-string and concatenate a space on the end (end of the word as well).
string word2 ="";
for (int i = 0; i<word.length-1;i++) {
word2 = word.substring(i, i+1)+" ";
}
I am using following code:
Arrays.stream(yourString.split(" ")).forEach( element -> {
// do something with each element
System.out.println(element + "\n");
});
Related
I have a problem. I want to type in a string (with Java.util.scanner) with two words. Then I want the program to split my entered string at the whitespace, save both substrings in a seperate variable and make a output in the end.
I know that you can split strings with
String s = "Hello World";
String[] = s.split(" ");
But it doesnt seem to work when your String is
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Any help?
Thank you very much
s.split("\\s+"); will split your string, even if you have multiple whitespace characters (also tab, newline..)
You can also use from java.util package
StringTokenizer tokens = new StringTokenizer(s.trim());
String word;
while (tokens.hasMoreTokens()) {
word = tokens.nextToken();
}
Or from Apache Commons Lang
StringUtils.split(s)
Your code works for me:
String s;
s=sc.nextLine();
String[] words=s.split(" ");
for(String w:words){
System.out.print(w+" ");
}
input: "Hello world"
output: "Hello world"
You may also want to try this way of splitting String that you get from user input:
Scanner sc = new Scanner(System.in);
String[] strings = sc.nextLine().split("\\s+");
If you simply want to print array containing these separated strings, you can do it without using any loop, simply by using:
Arrays.toString(strings);
If you want to have your printed strings to look other way, you can use for it simple loop printing each element or by using StringBuilder class and its append() method - this way may be faster than looping over longer arrays of strings.
How can I avoid of StringIndexOutOfBoundsException in case when string starts with space (" ") or when there're several spaces in the string?
Actually I need to capitalize first letters of the words in the string.
My code looks like:
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
String[] array = s.split(" ");
for (String word : array) {
word = word.substring(0, 1).toUpperCase() + word.substring(1); //seems that here's no way to avoid extra spaces
System.out.print(word + " ");
}
}
Tests:
Input: "test test test"
Output: "Test Test Test"
Input: " test test test"
Output:
StringIndexOutOfBoundsException
Expected: " Test Test test"
I'm a Java newbie and any help is very appreciated. Thanks!
split will try to break string in each place where delimiter is found. So if you split on space and space if placed at start of the string like
" foo".split(" ")
you will get as result array which will contain two elements: empty string "" and "foo"
["", "foo"]
Now when you call "".substring(0,1) or "".substring(1) you are using index 1 which doesn't belong to that string.
So simply before you do any String modification based on indexes check if it is safe by testing string length. So check if word you are trying to modify has length grater than 0, or use something more descriptive like if(!word.isEmpty()).
A slight modification to Capitalize first word of a sentence in a string with multiple sentences.
public static void main( String[] args ) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
int pos = 0;
boolean capitalize = true;
StringBuilder sb = new StringBuilder(s);
while (pos < sb.length()) {
if (sb.charAt(pos) == ' ') {
capitalize = true;
} else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) {
sb.setCharAt(pos, Character.toUpperCase(sb.charAt(pos)));
capitalize = false;
}
pos++;
}
System.out.println(sb.toString());
}
I would avoid using split and go with StringBuilder instead.
Instead of splitting the string, try to simply iterate over all characters within the original string, replacing all characters by its uppercase in case it's the first character of this string or if its predecessor is a space.
Use a regex in your split split all whitespaces
String[] words = s.split("\\s+");
Easier would be to use existing libraries: WordUtils.capitalize(str) (from apache commons-lang).
To fix your current code however, a possible solution would be to use a regex for words (\\w) and a combination of StringBuffer/StringBuilder setCharAt and Character.toUpperCase:
public static void main(String[] args) {
String test = "test test test";
StringBuffer sb = new StringBuffer(test);
Pattern p = Pattern.compile("\\s+\\w"); // Matches 1 or more spaces followed by 1 word
Matcher m = p.matcher(sb);
// Since the sentence doesn't always start with a space, we have to replace the first word manually
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
while (m.find()) {
sb.setCharAt(m.end() - 1, Character.toUpperCase(sb.charAt(m.end() - 1)));
}
System.out.println(sb.toString());
}
Output:
Test Test Test
Capitalize whole words in String using native Java streams
It is really elegant solution and doesnt require 3rd party libraries
String s = "HELLO, capitalized worlD! i am here! ";
CharSequence wordDelimeter = " ";
String res = Arrays.asList(s.split(wordDelimeter.toString())).stream()
.filter(st -> !st.isEmpty())
.map(st -> st.toLowerCase())
.map(st -> st.substring(0, 1).toUpperCase().concat(st.substring(1)))
.collect(Collectors.joining(wordDelimeter.toString()));
System.out.println(s);
System.out.println(res);
The output is
HELLO, capitalized worlD! i am here!
Hello, Capitalized World! I Am Here!
How to split a sentence into two parts in JAVA? If there is the following
String sentence = "I love Java <=> I love Python"
How can I return I love Java and I love Python thus separately ignoring <=>?
public void changeSentence(String line)
{
String[] words = line.split(" ");
for(int i = 0; i < words.length; i++)
{
if(!(words[i].equals("<=>")))
{
}
}
}
It can be done using the method given below of class String
METHOD: (public String[] split(String regex, int limit)
Regex: The String/Character you wish to remove & split remaining text
Limit: How many String that should be returned
public class TestSplit
{
public static void main(String args[])
{
String str = new String("I Love Java <=> I Love Python");
for (String retval: str.split("<=> ",2))
{
System.out.println(retval);
}
}
}
Output:
I Love Java
I Love Python
There are some other facts I am aware about are listed below
If you won't specify limit by 'keeping it blank'/'specify 0' then the compiler will split string every time '<=>' is found
e.g.
public class TestSplit
{
public static void main(String args[])
{
String str = new String("I Love Java <=> I Love Python <=> I Love Stackoverflow");
for (String retval: str.split("<=> "))
{
System.out.println(retval);
}
}
}
Output:
I Love Java
I Love Python
I Love Stackoverflow
Why not do:
String[] words = line.split("<=>");
for(String word : words){
System.out.println(word);
}
Output:
I love Java
I love Python
public String[] changeSentence(String line){
String[] substrings = line.split("<=>");
return substrings;
}
You can also split with StringTokenizer.
The code for splitting the strings based upon delimiter is below:
StringTokenizer stringTokenizer = new StringTokenizer(sentence,"<=>");
while(stringTokenizer.hasMoreTokens()) {
System.out.println(stringTokenizer.nextToken());
}
I hope this helps
Thanks
How can I return I love Java and I love Python thus separately ignoring <=>?
First of all as you have said that you want your method to return separate words
(Strings technically), for that you need change your return type from void to String[ ]
Second, you are using
String[] words = line.split(" ");
this will split the String where spaces appear which would yield you array of Strings containing
I
love
Java
<=>
I
love
Python
as different words stored as separate Strings in your words array.
so what you should do is
Strings[] words=line.split("<=>");
and return words
Full code should be like this
public String[] changeSentence(String line)
{
String[] words = line.split("<=>");
return words;
}
I'm in project doing a text mining. it's needed that my program also tokenize when the text using Enter in his/her document (/br if in HTML). Now my program only can detect 'space'. How to do it?
this is my code:
private ArrayList tokenize(String inp) {
ArrayList<String> out = new ArrayList<String>();
String[] split = inp.split(" ");
for (int i = 0; i < split.length; i++) {
if (!split[i].isEmpty()) {
out.add(split[i]);
}
}
return out;
}
You could also use a simple regular expression to do what you want:
String input = "Line of text \nAnother line<br><br><br />html<br />line";
String [] parts = input.split("\\s+|(<br>|<br\\s*/>)+");
System.out.println(Arrays.asList(parts));
It can also replace multiple whitespaces/breaklines in a row. Regular expressions can work really well for this kind of tasks.
Output:
[Line, of, text, Another, line, html, line]
Explanation: \s is short for all whitespaces (space, tab, newline). \s+ means 1 or more whitespaces. <br>|<br\\s*/> means <br> or <br/> or <br /> or <br />. They are in a group: (<br>|<br\\s*/>), so we can use + to identify one or more of them.
The whole stuff together: one or more whitespace characters or one or more of the different versions of <br>.
So your tokenize method could look like this (use generics, if you use java 1.5 or later):
private List<String> tokenize(String inp) {
List<String> out = new ArrayList<String>();
String[] split = inp.split("\\s+|(<br>|<br\\s*/>)+");
for (String s : split) {
if (!s.isEmpty()) {
out.add(s);
}
}
return out;
}
Are you sure splitting at the enters isn't already working? Because with this:
String s = "Hi b\nb bye";
System.out.println(s);
System.out.println();
String [] ss = s.split(" ");
for(String s2 : ss)
{
System.out.println(s2);
}
This is my output:
Hi b
b bye
Hi
b
b
bye
As you can see, the string was split both at the spaces, and at the new line (even though a space was the only regex). However, if this isn't working for you, you could just cycle through the String array and and call String.split("\n"). Then you can just add the new split strings to an ArrayList.
I would like to know how to split up a large string into a series of smaller strings or words.
For example:
I want to walk my dog.
I want to have a string: "I",
another string:"want", etc.
How would I do this?
Use split() method
Eg:
String s = "I want to walk my dog";
String[] arr = s.split(" ");
for ( String ss : arr) {
System.out.println(ss);
}
As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:
String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String[] words = s.split("\\W+");
The regex means that the delimiters will be anything that is not a word [\W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.
A regex can also be used to split words.
\w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:
String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
I
want
to
walk
my
dog
and
why
not
See Java API documentation for Pattern
See my other answer if your phrase contains accentuated characters :
String[] listeMots = phrase.split("\\P{L}+");
Yet another method, using StringTokenizer :
String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);
while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
To include any separators between words (like everything except all lower case and upper case letters), we can do:
String mystring = "hi, there,hi Leo";
String[] arr = mystring.split("[^a-zA-Z]+");
for(int i = 0; i < arr.length; i += 1)
{
System.out.println(arr[i]);
}
Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].
You can use split(" ") method of the String class and can get each word as code given below:
String s = "I want to walk my dog";
String []strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}
This regex will split word by space like space, tab, line break:
String[] str = s.split("\\s+");
Use split()
String words[] = stringInstance.split(" ");
StringTokenizer separate = new StringTokenizer(s, " ");
String word = separate.nextToken();
System.out.println(word);
Java String split() method example
public class SplitExample{
public static void main(String args[]){
String str="java string split method";
String[] words=str.split("\\s");//splits the string based on whitespace
for(String word:words){
System.out.println(word);
}
}
}
you can use Apache commons' StringUtils class
String[] partsOfString = StringUtils.split("I want to walk my dog", StringUtils.SPACE)
class test{
public static void main(String[] args){
StringTokenizer st= new StringTokenizer("I want to walk my dog.");
while (st.hasMoreTokens())
System.out.println(st.nextToken());
}
}
Using Java Stream API:
String sentence = "I want to walk my dog.";
Arrays.stream(sentence.split(" ")).forEach(System.out::println);
Output:
I
want
to
walk
my
dog.
Or
String sentence2 = "I want to walk my dog.";
Arrays.stream(sentence2.split(" ")).map(str -> str.replace(".", "")).forEach(System.out::println);
Output:
I
want
to
walk
my
dog
String[] str = s.split("[^a-zA-Z]+");