This question already has answers here:
Calculating frequency of each word in a sentence in java
(20 answers)
Closed 8 years ago.
I have read in two txt files with words.
Now I was wondering how to calculate the frequency distribution of these two Strings. My idea is to put them into an array and split them by ;. However, how to calculated the frequency distribution?
I appreciate your reply!
Sample code is something like this:
String str1 = "java;python;javascript;programming;Hello;World;Hello";
String str2 = "java;python;javascript;programming;Hello;World;Hello";
List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(str1.split(";")));
list.addAll(Arrays.asList(str2.split(";")));
for (String word : list)
System.out.println(word + " --> " + Collections.frequency(list,word));
Related
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.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a delimited string and The delimiter is "$%". The fields are in an specific order. For example: John$%Smith$%30$%Los Angeles
I need to get the values of this string and store them in the respective property
Customer.firstName(_)
Customer.lastName(_)
Customer.age(_)
Customer.city(_)
So far I have tried this but I know I am doing it incorrectly:
if(thisString != null){
if(thisString.endsWith("$%")){
Customer.firstName(thisString.substring(0,indexOf("$%");
}
}
Trying using Java's String.split
For example:
//Split string based on "$%"
String[] values = thisString.split(Pattern.quote("$%"));
Customer.firstName() = values[0]; //Set first name equal to first string from the split
//etc
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
I have a string "10 = On Battery ", I have to split the numeric value 10 and words "On Battery"(Including the space in between them) and store it in separate variables. Please help me with some example java code.
Try like this
String originValue = "10 = On Battery";
String [] splitedValue = originValue.split("/s=/s");
System.out.println(splitedValue[0]); // 10
System.out.println(splitedValue[1]); // On Battery
Try with:
String s= "10 = On Battery ";
String split[]=s.split("=");
split[0] will have 10 and split[1] will have On Battery
You can achieve this by using Stirng.split. Also It is very simple thing you try to learn String methods for like wise manipulation.
After considering your String It made me to add one thing
String s2= "10 = On Battery ";
String s[]=s2.split("=");
int i=Integer.parseInt(s[0].trim());<-----------
use trim() otherwise you will have "10 " with whitespace
^
But......
I have to split the numeric value 10 and words "On Battery"(Including
the space in between them)
String a=s2.split("=")[0];//"10 "
String b=s2.split("=")[1];//" On Battery "
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"}
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+");
}