2015-09-23 55 views
0

我无法编译此方法。此方法用于搜索类型为Event的数组。所以说,如果月份包含[1,2,3,4,5,6,7 * 8,9 *],它将搜索有星号的那些并返回true类型数组中的布尔方法

public static boolean isSignificant(Event[] month, String SearchValue) 
     { 
     boolean isFound = false; 
     for(int i = 0; i< month.length && isFound == false; i++) 
     { 

      if(month[i].contains(SearchValue)) // error on this line 
      { 
      isFound = true; 
      } 
     } 
     return isFound; 
     } 
+2

为什么期望'month [i] .contains(SearchValue)'编译? 'month'是一个数组,而不是'List'。你不能直接在数组上调用'contains'。另外,'month'在你搜索'String'的时候是'Event'类型 - 这是没有意义的。 –

回答

1

有很多方法来搜索这种模式

  1. if (value.endsWith("*")) {
  2. if (value.matches(".*\\*$")) {
  3. value.matches(".*?\\*$")

public class HelloWorld 
{ 
    static String[] month = new String[]{"1","2","3","4","5","6","7*","8","9*"}; 
    public static boolean isSignificant() 
     { 
      boolean isFound = false; 
      for(int i=0; i <month.length && isFound == false; i++) 
       { 
        if(month[i].endsWith("*")) 
         { 
          isFound = true; 
         } 
       } 
      return isFound; 
     } 

    public static void main(String []args) 
     { 
      HelloWorld obj = new HelloWorld(); 
      if(obj.isSignificant()) 
       { 
        System.out.println("The string ends with *"); 
       } 
      else 
       { 
        System.out.println("The string donot end with *"); 
       } 
     } 
} 
+1

谢谢:)正是我需要的 –

0

@ Jean-FrançoisSavard是对的,月份是类型事件,而你正在寻找一个字符串。如果你向我解释更多的信​​息,我可以帮助你更多,否则生病只是假设你的意思是一个字符串数组。

public class HelloWorld{ 

    public static void main(String []args){ 
     System.out.println("Hello World"); 
     String [] array = {"1","2","3","4*"} ; 
     if(isSignificant(array,"*")){ 
      System.out.println("Found"); 
     }else{ 
      System.out.println("Not found"); 
     } 
    } 

    public static boolean isSignificant(String[] month, String SearchValue) { 
     for(int i = 0; i< month.length; i++) { 

      if(month[i].contains(SearchValue)) { 
      return true; 
      } 
     } 
     return false; 
     } 
}