[英]Checking if String Array is filled or empty
希望這不是重復的,因為我已經在其中查找了一些線程。 這個 ,但是對我沒有幫助。
我的程序正在讀取一些用戶可選的參數。 他們可以為游戲添加規則,但不必這樣做。 我知道rule
將包含5 Numbers
。 我想將它們保存在String Array with 5 spots
的String Array with 5 spots
以便以后使用。 如果用戶不輸入規則,則會采用特定的規則。
String[] rule = new String[5];
//reading in the program arguments and stuff..
//here I want to check whether the rule is taken from the user or not
//don't want to check with a boolean check
if (rule[0].equals("")) {
String[] newRule = "270-90-315-45-90".split("-");
for (int i = 0; i < newRule.length; i++) {
rule[i] = newRule[i];
}
}
已經嘗試過:
rule[0].equals("")
rule[0].equals(null)
rule[0].equals("null")
rule[0].matches("")
rule[0].matches("null")
rule.length == 0
rule.equals(null)
但是我總是收到NullPointerException,否則將跳過if情況(長度== 0)
希望您能夠幫助我。
您沒有嘗試過顯而易見的事情嗎?
if (rule[0] == null) {
在為您提供的示例中, rule[0]
包含null
,您可以通過添加以下代碼行來看到自己:
System.out.println(rule[0]==null);
返回true
(嘗試!)
if (rule[0]==null) {
如果需要的話,將返回true
並進入for循環。請參見以下可compile
(使用javac myEmpty.java
)並run
(使用java myEmpty
)的類:
class myEmpty {
public static void main(String[] args) {
String[] rule = new String[5];
System.out.println(Arrays.toString(rule));
//reading in the program arguments and stuff..
//here I want to check whether the rule is taken from the user or not
//don't want to check with a boolean check
System.out.println(rule[0] == null);
if (rule[0] == null) {
//if ("".equals(rule[0])) {
String[] newRule = "270-90-315-45-90".split("-");
System.out.println(Arrays.toString(newRule));
for (int i = 0; i < newRule.length; i++) {
rule[i] = newRule[i];
System.out.println(rule[i]);
}
}
}
}
您的if if (rule[0].equals("")) {
失敗僅是因為rule[0]
不包含您要檢查的值 ""
! 請記住, ""
和null
不相同, 請相應地使用if
子句 !
因此,您只想更改用戶輸入的索引。
最好的方法是使用默認值初始化數組, 然后覆蓋用戶指定的值。
String[] rule = "270-90-315-45-90".split("-");
// Now read "program arguments and stuff"
考慮使用固定數組,請嘗試使用ArrayList並將值添加到列表中。 ArrrayList類將自動調整列表的長度。
例:
ArrayList<String> rules = new ArrayList<>();
if (rules.size() == 0) {
String[] newRule = "270-90-315-45-90".split("-");
//Instead of newRule.length as a comparison, use the
//number of rules with a number. Or use a foreach-loop
//if the number of rules may vary
for (int i = 0; i < 5; i++) {
rules.add(newRule[i]);
}
}
我想象的NullPointerException來自以下事實:您創建了一個字符串數組,但尚未使用任何值初始化它們。 如果您不給它們一個值,它們將默認為null ...因此NPE。
根據您在哪里獲得輸入,您可以執行以下操作:
private static final String[] DEFAULT_RULES = {"270", "90", "315", "45","90" };
private static String[] rules;
public static void main(String[] args){
if(!isValidRule(args)){
// Potentially also check that the args are digits
throw new IllegalArgumentException("Must contain 5 rules");
}
rules = (args.length > 0) ? args : DEFAULT_RULES; // Used brackets on the ternary operator here for readability. Not essential.
...
}
private static boolean isValidRule(String[] rules){
return rules.length > 0 && rules.length != 5;
}
如果您正在使用其他一些采用輸入的非靜態方法,則同樣適用。 您可以執行字符串拆分以根據指定的分隔符獲取數組,然后執行相同的操作。
我不認為如果不傳遞任何規則,您是否想傳遞僅包含連字符的字符串? 通過嘗試在執行拆分后檢查字符串是否為空,可以暗示這是什么。
另外,如果要檢查字符串是否包含字符,請使用isEmpty()
方法。 如果長度為0,則返回true,否則返回false。 這已經可以實現您不必要的嘗試。
在每個您嘗試過的選項中
rule[0].equals("")
rule[0].equals(null)
rule[0].equals("null")
rule[0].matches("")
rule[0].matches("null")
rule.length == 0
rule.equals(null)
您已經假定索引為0
的元素不為null,並且對索引為0
的元素調用equals()
和matches()
方法會導致NullPointerException
當應用程序在需要對象的情況下嘗試使用null時拋出。 這些包括:
- 調用空對象的實例方法。
- 訪問或修改空對象的字段。
- 將null的長度視為數組。
- 訪問或修改null插槽,就好像它是一個數組一樣。
- 將null拋出,就好像它是一個Throwable值一樣。
像這樣嘗試
if(rule[0] != null){
// Now you can call methods on your `0th` index element
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.