2016-09-16 114 views
0

今晚再次您好!我試圖創建一個錯誤消息,如果字符串的輸入不包含單詞/字符:是,是,否,否,Y,Y,N,N;並循環返回以獲取正確的輸入。任何建議,非常感謝! :)在字符串中匹配某些字詞和字符

do{ 

addAnother = JOptionPane.showInputDialog("Would you like to enter another salespersons' data?"); 

if (!addAnother.matches("[YesYESNonoYyNn]")) 
    System.out.println("Valid answers: Yes, YES, No, no, Y, y, N, n"); 
} 
while (addAnother.matches("[YesYESNonoYyNn]")); 
+0

嘗試使用正則表達式? **「^((是)|(y)|(no)|(n))$」**通過先從兩端刪除空格,然後將** addAnother **變量先轉換爲小寫? – blackpen

回答

2

不要打擾使用正則表達式爲此。設置一個得到了所有你想匹配的HashSet和使用contains

所以外的所有類的方法中的字符串常量,你就會有

private static final Set<String> validOptions = new HashSet<>(Arrays.asList("Yes", "YES", "No", "no", "Y", "y", "N", "n")); 

,並驗證,你只有

while (true) { 
    addAnother = JOptionPane.showInputDialog("Would you like to enter another salespersons' data?"); 

    if (validOptions.contains(addAnother)) { 
     break; 
    } 

    System.out.println("Not a valid option"); 
} 

更新

有很多方法來改善這種, 當然。 @blackpen在評論中提出了一對夫婦。一個非常好的方法是將所有這些分解到單獨的方法中,並且實際返回true或false來指示用戶對問題回答是或否。所以你的班級可能包括所有這些。

public class GregsFunkyClass { 

    private static final Map<String,Boolean> validOptions = new HashMap<>(); 
    static { 
     validOptions.put("yes", true); 
     validOptions.put("y", true); 
     validOptions.put("no", false); 
     validOptions.put("n", false); 
    } 

    private boolean shouldEnterMoreData() { 
     while (true) { 
      String input = JOptionPane.showInputDialog("Would you like to enter another salespersons' data?"); 
      boolean toReturn = validOptions.get(input.trim().toLowerCase()); 
      if (toReturn != null) { 
       return toReturn; 
      } 
      System.out.println("Not a valid option"); 
     } 
    } 
} 

然後就可以調用在類其他方法這個方法,真正得到你要找的答案。在這裏,調用trim()將刪除前導空格和尾部空格,並且調用toLowerCase()會使混合大小寫或大寫輸入匹配我們實際放入映射中的值。

注意 - 你做了一些奇怪的事情,把控制檯輸出與GUI輸入混合起來。通常,應用程序是GUI或控制檯應用程序,但不是混合應用程序。如果用戶輸入了錯誤的選項,您可能希望實際在誤操作時輸出錯誤消息,而不是在控制檯上。我沒有在這裏做過,但希望你能明白。

+0

好的。是的,可能比正則表達式更容易。但是,注意用戶是否輸入了前導/尾隨空格......並且如果他使用了混合大小寫字母。 – blackpen

+0

也可能你想要小寫所有的散列元素以及小寫用戶的輸入,以便它們完全匹配? – blackpen

+0

但是,我想你仍然想處理混合鍵入(YeS,nO)?是的,OP最終需要檢查明確的(是)和否定的(否)。因爲,積極的,他循環;負面的,他退出。 – blackpen

0

除了@大衛的不錯的答案,爲了以防萬一,如果你仍然想使用正則表達式(?低效),這裏是你如何能做到這一點:

public class Main { 

    public static boolean isYes(String str) { 
     String s1=str.trim(); 
     return(s1.matches("(?i:^((yes)|(y))$)")); 
    } 

    public static boolean isNo(String str) { 
     String s1=str.trim(); 
     return(s1.matches("(?i:^((no)|(n))$)")); 
    } 

    public static void main(String []args) { 

     System.out.println(isYes(" yes ")); 
     System.out.println(isYes(" yEs ")); 
     System.out.println(isYes(" y")); 

     System.out.println(isNo(" yes ")); 
     System.out.println(isNo(" yEs ")); 
     System.out.println(isNo(" y")); 

     //-------------------------------- 

     System.out.println(isNo(" NO ")); 
     System.out.println(isNo(" no ")); 
     System.out.println(isNo("n ")); 

     System.out.println(isYes(" NO ")); 
     System.out.println(isYes(" no ")); 
     System.out.println(isYes("n ")); 

    } 
} 

輸出:

true 
true 
true 
false 
false 
false 
true 
true 
true 
false 
false 
false