Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Darin Thompson
5,305 PointsHow to use regular for loop instead of 'Enhanced for loop'
While watching this vide and coding along on my own machine I realized that the example uses an enhanced loop instead of a regular for loop. I have been trying and searching for hours trying to get it to work using a regular loop and I can't seem to find the solution.
This is my code. Any help would be appreciated.
*I ran the code just fine when following the tutorial so I know the error isn't in another file or anything like that.
import java.util.*;
import com.teamtreehouse.Treet;
public class Example {
public static void main(String[] args) {
Treet treet = new Treet(
"darinthompson",
"@This is my first #tweet from my tweet app",
new Date(1494354142L)
);
System.out.println("The words are:");
String[] tweetArray = treet.toLowerCase().getWords();
for (int i = 0; i < tweetArray.length; i++) {
System.out.println("- " + i);
}
}
}
2 Answers

Fahad Mutair
10,359 Pointshi Darin, the Enhanced for loop
//... Foreach loop over all elements in arr.
for (type var : arr) {
body-of-loop
}
and equivalent for statements
//... For loop using index.
for (int i = 0; i < arr.length; i++) {
type var = arr[i];
body-of-loop
}
here's example
double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (double d : ar) { // d gets successively each value in ar.
sum += d;
}
double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (int i = 0; i < ar.length; i++) { // i indexes each element successively.
sum += ar[i];
}
might that helps

Darin Thompson
5,305 PointsJazakala Khair habibi! <--- I currently live in the UAE and I am learning arabic, so I figured I would show off a little bit.
Anyway, thanks a lot for the help. Keeping track of the type of variables in Java is hard to do I guess. I can't ever seem to get it right.
Ma asalama!

Fahad Mutair
10,359 Pointshahaha you're more than welcome
Fahad Mutair
10,359 PointsFahad Mutair
10,359 Points