Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop error

 
chandra reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you compiling it with Java 5.0?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please read our JavaRanch Naming Policy and change your display name to comply with it.
Thanks
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic