java split sentences into words [duplicate] - java

This question already has answers here:
split file sentences into words
(4 answers)
Closed 10 years ago.
How to split an array of sentences into an array of array of words?
If i can split the sentence using split() ..but it's used only single array.
but I need to do multiple array..
Eg:
sentences[0]="one sentence"
sentences[1]=" one sen...."
I need to split like this...
word[0][0]="one word" //first row first word
word[0][1]="second word"
word[0][2]="third word"
word[1][0]="..."//second row first word**
any one can help me.

Try something like this..
for(i=0;i<someLength;i++){
word[i] = sentence[i].split("yourDelimiter");
}

String[] sentences = ...
String[][] words = new String[sentences.length][];
for(int i = 0; i < sentences.length; i++)
{
words[i] = sentences[i].split("\\s+");
}

Related

Make ArrayList with words that start with specific letter? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I am trying to make a method that takes in an ArrayList and a letter. If the words in the arraylist start with that letter, than it will be put into a new array of words that start with that letter. For example, an arraylist with ("Apple", "Anny", "Bob") and a letter of "A" would create a new arraylist ("Apple", "Anny"). I am not allowed to use .startsWith(char ch)
public ArrayList<String> wordsThatStartWith(ArrayList<String> words, String letter)
{
ArrayList<String> newWords = new ArrayList<String>();
for(int i = 0; i < words.size(); i++){
if((words.get(i)).substring(i, i + 1) == letter){
newWords.add(words.get(i));
}
}
return newWords;
}
I am not sure why it will not add into a new ArrayList.
Replace
if((words.get(i)).substring(i, i + 1) == letter)
with
if((words.get(i)).substring(0, 1).equalsIgnoreCase(letter))
as you need to get just the first character from the string.
Using the enhanced for loop, you can write it as
for(String word: words) {
if(word.substring(0, 1).equalsIgnoreCase(letter)){
newWords.add(word);
}
}

java split function. Store in an array from custom index? [duplicate]

This question already has answers here:
How I can index the array starting from 1 instead of zero?
(6 answers)
Closed 5 years ago.
I want to split a string and store in an array from custom index and NOT from "0" index by default.
Eg:
String splitThis = "cat,dog";
String [] array = splitThis.split(",");
System.out.println array[0] + array[1]
Above code prints "catdog" but I want "cat" to be store in index "1" and "dog" in index "2"
PS: I am very new to Programming and this is my very first question. Please correct me in syntax/logic/whatever :)
You may just add an empty entry at index 0. Something like this
String splitThis = "cat,dog";
String spit2 = ","+splitThis;
String [] array = split2.split(",");
System.out.println (array[1]);
System.out.println (array[2]);
You should probably create an entirely new class to handle that.

Find the longest String prefix [duplicate]

This question already has answers here:
Find longest common prefix?
(12 answers)
Closed 6 years ago.
Write a function to find the longest prefix of a list of string. For example,
['abc', 'abcde', 'abxyz'] => 'ab'
So it is an Arraylist and we find the longest prefix in the list of strings.
Let's try Java.
Please, no complete solutions
public string prefix (Arraylist<String> lst){
Arraylist<char[]> charLst = new Arraylist<>;
for(int i =0; i < lst.size(); i++){
charLst.add(lst.get(i).toCharArray());
}
}
But how do I proceed after creating a CharArray? This is already starting to be inefficient as it is O(n) with just the conversion to CharArray. I would just like some hint/help in the approach
Why do you add the ith character of every string to charLst? What you need is just the length of the longest common prefix, and then you can output the prefix based on the length you got.

Count Words from a string containing \n [duplicate]

This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 6 years ago.
I have below mentioned string
String str = "\nArticle\n\nArticle\nArticle";
I want total number of count. How can i get this?
As the string contain \n so always it gives 1 instead of 3
To get you started, I will show you a simple example:
String str = "\nArticle\n\nArticle\nArticle";
// Split the String by \n
String[] words = str.split("\n");
// Keep the count of words
int wordCount = 0;
for(String word : words){
// Only count non-empty Strings
if(!word.isEmpty()) {
wordCount++;
}
}
// Check, answer is 3
System.out.println(wordCount);

Split a string, two words at a time [duplicate]

This question already has answers here:
Java. Splitting a multiple word string into two word strings every space [closed]
(2 answers)
Closed 9 years ago.
I have
String input = "one two three four five six seven";
Is there a regex that works with String.split() to grab (up to) two words at a time, such that:
String[] pairs = input.split("some regex");
System.out.println(Arrays.toString(pairs));
results in this:
[one two,two three, three four,four five,five six,six seven]
String[] elements = input.split(" ");
List<String> pairs = new ArrayList<>();
for (int i = 0; i < elements.length - 1; i++) {
pairs.add(elements[i] + " " + elements[i + 1]);
}
No. With String.split(), the things you get out can't overlap.
e.g. you can get: "one two three four" -> {"one","two","three","four"}, but not {"one two","two three", "three four"}

Categories