permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "for loop error" Watch "for loop error" New topic
Author

for loop error

chandra reddy
Greenhorn

Joined: Apr 11, 2007
Posts: 10
import java.util.*;
class SplitTest {
public static void main(String[] args) {
String[] tokens = args[0].split(args[1]);
System.out.println("count " + tokens.length);
for(String s : tokens)
System.out.println(">" + s + "<");
}
}
I am getting compilation error in the for statement .
Could any body explain how this for loop works.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
hi
chandu
I have executed your program ,i am not getting any error
could you share that error .

And i have some probelm with the split method have you understood that one.
Please see

PROBLEM IN SPLIT METHDOD in the main page
chandra reddy
Greenhorn

Joined: Apr 11, 2007
Posts: 10
Hello Anil,
i understood the split method , example abc5xya6adf9764 suppose we are spliting based on digit then it will split in to abc,xya and adf .so it also doing same it is not considering lost 764 , it consider if any nondigits only.

coming to my doubt am not understanding the the new format of for loop .
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Are you compiling it with Java 5.0?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
And please read our JavaRanch Naming Policy and change your display name to comply with it.
Thanks
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
hi
chandu

What are you not able to understand?

The for loop is like this
if it is an int array the loop variable should be int like
int[] a={1,2,3,4};
for(int i:a)
System.out.println(i);

here i contains the value of a according to the index
Thanks
[ April 13, 2007: Message edited by: anil kumar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop error