-2

Programming in Android/Java. I get both the log error message and the popup alert when string length is both correct or incorrect. I have attempted to flip the if-else statement and use (txtVin.length() ==17) but that isn't working either. Any and all help would be appreciated.

There is a separate Java class file for the VinConstants.

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 is a Vehicle Identification Number as a string taken from an EditText field:

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

        }
    }
}
}
  • Please read [mcve] and enhance your question accordingly. No idea what txtVin is, or is supposed to contain... – GhostCat Jul 05 '18 at 13:34
  • update your full code – Shiv Jalkote Jul 05 '18 at 13:34
  • Can you at least show us what value is in `txtVin` and in `weightDigit`? – achAmháin Jul 05 '18 at 13:42
  • Why are you alerting *VIN is too short!* for every case `txtVin.length() != 17`? It will show the alert for longer strings, too! Write `txtVin.length() < 17` instead and apply handling for `== 17` and `> 17`... – deHaar Jul 05 '18 at 13:44
  • In Android layout I have restricted the maximum character length to 17 so it should never be longer than 17 characters which is why I am alerting for every case of `txtVin.length() !=17`. I tried using `txtVin.length() < 17` but that produces the same issue. – Brian McMillen Jul 05 '18 at 14:05

1 Answers1

0

You are populating txtVin in onCreate and not when your button is pressed. It is likely that the string is empty.

Set txtVin in your check method instead of 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();

        }
    }
}
Jon
  • 9,156
  • 9
  • 56
  • 73
  • This didn't work, however, when I moved both the vin to String and txtVin to the method, I stopped getting the error. Thanks!!! – Brian McMillen Jul 06 '18 at 13:54