字符串长度正确,但仍然出现错误

时间:2018-07-05 13:32:28

标签: java android if-statement string-length

使用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();

        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

您正在onCreate中填充txtVin,而不是在按下按钮时填充。字符串很可能为空。

在您的检查方法中设置txtVin而不是onCreate():

public void checkVIN() {
    String txtVin = vin.getText().toString();
    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();

        }
    }
}