简体   繁体   中英

How to use if else for an array in java

I want to use an if condition on my array.
Here is my code:

String[] result = value_got.split(" ");    
String fabric = result[5];

I want to check first to see if result[5] has a value or not.
Because when it has a value, the app works correctly, but when it has no value, my app keeps stopping.

You may first check the length of the array resulting from the split:

String[] result = valueGot.split(" ");
String fabric = result.length >= 6 ? result[5] : "";

Note that Java naming conventions generally frowns upon putting underscores in variable names. Instead of using value_got , use valueGot , in camel case.

I want to use an if condition on my array.
Here is my code:

String[] result = value_got.split(" ");    
String fabric = result[5];

I want to check first to see if result[5] has a value or not.
Because when it has a value, the app works correctly, but when it has no value, my app keeps stopping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM