-2

After looking for a while, it's still bugging me:

I have a simple code where I want to retrieve data looking like:

data1/data2/style…

and I want to separate the data at each /. So I have written:

MyData = data.split("/")

and then:

for (i = 0; i < myData.size; i++) 

to iterate over the values. But I'm getting the following error:

no signature of method length for type argument: () values: []

so I'm assuming that myData is empty.

4
  • 1
    Use .size() to get the length of a list, not .length Commented May 21, 2014 at 10:10
  • 2
    try a foreach loop: for (String s : mesDonnees) { //use s } Commented May 21, 2014 at 10:11
  • @TimCastelijns what if OP's using an array but calling it list? Commented May 21, 2014 at 10:12
  • MyData = data.split("/") should be MyData myData = data.split("/") Commented May 21, 2014 at 10:15

2 Answers 2

13

if you want to iterate using an integer, you should use MyData.size() in the for loop.

But it is a better idea to do:

String[] myData = data.split("/");
for (String s: myData) {
    System.out.println(s);
}

to use each string of the array.

If the iteration only iterates once over your array, then it may be your string that has a problem. As a double check, you may do:

System.out.println(myData.size());

You may also want to add a breakpoint after the .split() and look using a debugger if the array really contains all the strings you're expecting it to contain.

Sign up to request clarification or add additional context in comments.

4 Comments

i replace .length by .size but it gets only the first data
N.B.: I did not write the corrected for(int i;…;…) version on purpose, because you should better use iterators instead of indexes as iterators protects you from typos or mistakes that can compile and throw an exception at runtime.
i try this too but same problem... i'm not really in java so i haven't fuction like system.out.println but i'm trying to print it otherway...
myData contains exactly what i need... i will continue to search, thanks for your help
2

I am having some trouble understanding your question, even with the translation :)

In the line

mesDonnees = maDonnee.split("/");

mesDonnees needs to be a String array (String[]) and you can loop through it like:

for (String str : mesDonnees) {
  //... do somwething with str
}

You can rename str something in French if you like, I couldn't think of a suitable name

3 Comments

actually it's always a bad idea to use variable names in french (or any other language than english). Because the keywords of the language and the name of the methods are in english and mixing languages will ultimately create confusion. N.B.: I'm not a native english speaker, and I've been fighting against that for many years.
I've always felt that non-native English speakers are discriminated against when it comes to software development because, as you say, all language keywords are in English. I was assuming that using your own language for variable names might help, but as you are a non-native English speaker, you would be a better judge than I.
All I'm praising for is consistency. Computers, languages, libraries and tools are all written in English for historical reasons first, but since then, for homogeneity. What if one day, you get to work on a code I have written with french variables, that another Hungarian guy took over and added hungarian variable names, which then has been patched by a korean guy who used korean names for variables? Won't you just loose your head? The level of english needed to write into a given language is somehow the same as the one needed for 90% of variables. For the 10% left there's google translate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.