array lengths differed, expected.length=1 actual.length=0 in Junit

时间:2015-05-12 22:55:16

标签: java arrays string junit

Write a method that will search an array of strings for all strings that contain another string, ignoring capitalization. Then return an array of the found strings.

The method takes two parameters, the query string and the array of strings to search, and returns an array.

If the string isn't contained in any of the strings in the array, the method returns an array containing a single string: "Empty".

Example: If the string to search for is "me", and the array to search is ["home", "milk", "Mercury", "fish"], the method should return ["home", "Mercury"].

That's my solution and it works in Eclipse, but in a game they display an error message:

array lengths differed, expected.length=1 actual.length=0

Code:

> mainDF= data.frame(name= c("a","b","c","d"))
> tail(mainDF,1)["name"]
  name
4    d
> dt<-as.data.table(mainDF)
> setkey(dt, name )
> tail(dt,1)["name"]
   name
1:   NA

I don't know what it means, I try to find it on Google but i can't find something useful for me.

1 个答案:

答案 0 :(得分:3)

array lengths differed, expected.length=1 actual.length=0

This means you're returning an array of length 0 when the correct return value is an array of length 1. The expected length is 1, and the length you actually returned is 0.

When do you return an array of length 0? It must be the last line of your function that's at fault since that's the only place you return an empty array.

Your code is failing this condition:

If the string isn't contained in any of the strings in the array, the method returns an array containing a single string: "Empty".

When there's no match you're returning sf.parse(dateString);. You need to return a 1-element array containing the string new String[0].