Reputation:
Can you please convert this enhanced for loop to an ordinary one?
for (String sentence : notes.split(" *\."))
I'm fond of enhanced and normal for loop when the data type is an integer. But if it is a string, I'm confused. Thank you very much!
Upvotes: 3
Views: 938
Reputation: 7899
split
returns you the String[]
.
String[] array=notes.split(" *\.");
for(int i=0;i<array.length();i++) {
System.out.println(array[i]);
}
Upvotes: 0
Reputation: 13151
String[] sentences = notes.split(" *\.");
String sentence = null ;
int sentencesLength = sentences.length;
for(int i=0;i<sentencesLength;i++){
sentence = sentences[i];
//perform your task here
}
Eclipse Juno has in-build feature for converting for-each to index based loop. Check it out.
Upvotes: 3
Reputation: 15204
You should take a look at For-Each's Doc.
String[] splitted_notes = notes.split(" *\. ");
for (int i=0; i < splitted_notes.length; i++) {
// Code Here with splitted_notes[i]
}
Or a loop more similar to for (String sentence : notes.split(" *\."))
ArrayList<String> splitted_notes = new ArrayList<>(Arrays.asList(notes.split(";")));
for(Iterator<String> i = splitted_notes.iterator(); i.hasNext(); ) {
String sentence = i.next();
// Code Here with sentence
}
Upvotes: 2
Reputation: 3288
I guess the regex itself is a wrong one. The compiler will say
illegal escape character
if
"*\."
is the regex. So I am assuming that you are trying to split a string by have having
. (a dot)
as a delimiter. In that case, the code would be like
String[] splittedNotes = notes.split("[.]");
for (int index = 0; index < splittedNotes.length; index++) {
String sentence = splittedNotes[index];
}
On a polite note, you could have tried and got this by yourself. Cheers.
Upvotes: 0
Reputation: 8014
Ordinary for loop -
String[] strArray = notes.split(" *\.");
String sentence = null;
for(int i=0 ;i <strArray.length ; i++){
sentence = strArray[i];
}
Upvotes: 1
Reputation: 1195
String [] array = notes.split(" *\."));
String sentence;
for(int i = 0; i < array.length; i ++) {
sentence = array[i];
}
Upvotes: 0
Reputation: 52185
You could have something like so:
String[] splitString = notes.split(" *\."));
for (int i = 0; i < splitString.length; i++)
{
//...
}
OR
for(String str : splitString)
{
//...
}
Upvotes: 0
Reputation: 2474
String[] splitResult=notes.split(" *\.");
for (String sentence : splitResult)
Upvotes: 1