2015-09-23 57 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; 
     } 
}