【发布时间】:2018-12-14 00:24:31
【问题描述】:
在 Android/Java 中编程。当字符串长度正确或不正确时,我会同时收到日志错误消息和弹出警报。我试图翻转 if-else 语句并使用 (txtVin.length() ==17) 但这也不起作用。任何和所有的帮助将不胜感激。
VinConstants 有一个单独的 Java 类文件。
public class VinConstants {
public static Map<String, Integer> YEAR_INDEX = new HashMap<>();
public static String YEAR_CHARS[] = {
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R"
, "S", "T", "V", "W", "X", "Y", "1", "2", "3", "4", "5", "6", "7"
, "8", "9", "0"};
public static final Map<String, Integer> ALPHABET_INDEX = new HashMap<>();
public static final String ALPHABET_CHARS[] = {
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R"
, "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7"
, "8", "9", "0"};
public static final Map<String, Integer> WEIGHTS = new HashMap<>();
public static final int ALPHABET_WEIGHTS[] = {
//A,B,C,D,E,F,G,H,J,K,L,M,N,P,R
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 7, 9,
//S,T,U,V,W,X,Y,Z,1,2,3,4,5,6,7
2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7,
//8,9,0
8, 9, 0};
public static int WEIGHT_FACTOR[] =
{8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2};
static {
int len = VinConstants.ALPHABET_CHARS.length;
for (int i = 0; i < len; i++) {
ALPHABET_INDEX.put(VinConstants.ALPHABET_CHARS[i], i);
}
for (int i = 0; i < len; i++) {
WEIGHTS.put(VinConstants.ALPHABET_CHARS[i], ALPHABET_WEIGHTS[i]);
}
len = VinConstants.YEAR_CHARS.length;
for (int i = 0; i < len; i++) {
YEAR_INDEX.put(VinConstants.YEAR_CHARS[i], i);
}
}
}
txt.VIN 是车辆识别号,取自 EditText 字段的字符串:
public class MainActivity extends AppCompatActivity {
String txtVin;
Button button;
EditText vin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText vin = (EditText) findViewById(R.id.vin);
txtVin = vin.getText().toString();
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
checkVIN();
getWeightDigit();
}
});
}
public char getWeightDigit() {
int prodSum = 0;
for (int i = 0; i < txtVin.length(); i++) {
String letter = txtVin.substring(i, i + 1);
int factor = VinConstants.WEIGHT_FACTOR[i];
int weight = VinConstants.WEIGHTS.get(letter);
prodSum += factor * weight;
}
if (prodSum % 11 == 10) {
return 'X';
} else {
return (char) ('0' + prodSum % 11);
}
}
public void checkVIN() {
if (txtVin.length() != 17) {
Log.d("VIN", "Invalid VIN Length");
AlertDialog.Builder alertInvalidLength = new AlertDialog.Builder(this);
alertInvalidLength.setMessage("VIN is too short!");
alertInvalidLength.setTitle("Warning:");
alertInvalidLength.setPositiveButton("OK", null);
alertInvalidLength.setCancelable(true);
alertInvalidLength.create().show();
} else {
// Validate checksum.
char weightDigit = getWeightDigit();
if (txtVin.charAt(8) != weightDigit) {
Log.d("VIN", "Invalid VIN!");
AlertDialog.Builder alertInvalidVIN = new AlertDialog.Builder(this);
alertInvalidVIN.setMessage("VIN is invalid!");
alertInvalidVIN.setTitle("Warning:");
alertInvalidVIN.setPositiveButton("OK", null);
alertInvalidVIN.setCancelable(true);
alertInvalidVIN.create().show();
}
}
}
}
【问题讨论】:
-
请阅读minimal reproducible example 并相应地增强您的问题。不知道 txtVin 是什么,或者应该包含什么......
-
更新您的完整代码
-
您能否至少告诉我们
txtVin和weightDigit中的值是什么? -
你为什么要提醒 VIN 太短! 对于每个案例
txtVin.length() != 17?它也会显示更长字符串的警报!改写txtVin.length() < 17并申请处理== 17和> 17... -
在 Android 布局中,我已将最大字符长度限制为 17,因此它永远不应超过 17 个字符,这就是为什么我会提醒
txtVin.length() !=17的每一种情况。我尝试使用txtVin.length() < 17但这会产生同样的问题。
标签: java android if-statement string-length