3

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!

1
  • It is written in our book and I'm curious what is it's counterpart in the normal one.
    – user1531959
    Jul 24, 2012 at 11:57

8 Answers 8

3
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.

2
  • Cool! Thank you so much, Priyank Doshi! And to others also.
    – user1531959
    Jul 24, 2012 at 12:15
  • 1
    Best practice : add n (do not evaluate at each loop) : for(int i=0, n=sentences.length;i<n;i++){ ..
    – cl-r
    Jul 24, 2012 at 12:22
2

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
}
7
  • sentence = splitted_notes[i++] ? no need for it inside the loop if i isn't actually being used
    – Basic
    Jul 24, 2012 at 12:08
  • you can pull the i++ part into the third part of for: ...; sentence = ..., i++)
    – A.H.
    Jul 24, 2012 at 12:08
  • instead of sentence = splitted_notes[i], i++ you can always use sentence = splitted_notes[i++] :)
    – Pshemo
    Jul 24, 2012 at 12:11
  • Basic i is being used. He just take each splitted_notes's string. But your code is leaner!
    – waldyr.ar
    Jul 24, 2012 at 12:13
  • Hmm.. Second though on the second suggestion: In the first iteration sentence is not the first sentence but "". AND you get an ArrayIndexOutOfBoundsException in the last iteration.
    – A.H.
    Jul 24, 2012 at 12:13
1
String[] splitResult=notes.split(" *\.");
for (String sentence : splitResult)
1

Ordinary for loop -

String[] strArray = notes.split(" *\.");
String sentence = null;
  for(int i=0 ;i <strArray.length ; i++){
    sentence = strArray[i];
   }
0
0

You could have something like so:

String[] splitString = notes.split(" *\."));
for (int i = 0; i < splitString.length; i++)
{
    //...
}

OR

for(String str : splitString)
{
    //...
}
0
String [] array = notes.split(" *\."));
String sentence;
for(int i = 0; i < array.length; i ++) {
sentence = array[i];
}
0

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.

0

split returns you the String[].

String[] array=notes.split(" *\.");
for(int i=0;i<array.length();i++) {

    System.out.println(array[i]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.