0

I have a scanner that reads a 7 character alphanumeric code (inputted by the user). the String variable is called "code".

The last character of the code (7th character, 6th index) MUST BE NUMERIC, while the rest may be either numeric or alphabetical.

So, I sought ought to make a catch, which would stop the rest of the method from executing if the last character in the code was anything but a number (from 0 - 9).

However, my code does not work as expected, seeing as even if my code ends in an integer between 0 and 9, the if statement will be met, and print out "last character in code is non-numerical).

example code: 45m4av7

CharacterAtEnd prints out as the string character 7, as it should. however my program still tells me my code ends non-numerically. I'm aware that my number values are string characters, but it shouldnt matter, should it? also I apparently cannot compare actual integer values with an "|", which is mainly why im using String.valueOf, and taking the string characters of 0-9.

String characterAtEnd = String.valueOf(code.charAt(code.length()-1));
System.out.println(characterAtEnd);

 if(!characterAtEnd.equals(String.valueOf(0|1|2|3|4|5|6|7|8|9))){
     System.out.println("INVALID CRC CODE: last character in code in non-numerical.");
     System.exit(0);

I cannot for the life of me, figure out why my program is telling me my code (that has a 7 at the end) ends non-numerically. It should skip the if statement and continue on. right?

SmashAdams
  • 13
  • 1
  • 4

2 Answers2

0

The String contains method will work here:

String digits = "0123456789";
digits.contains(characterAtEnd); // true if ends with digit, false otherwise

String.valueOf(0|1|2|3|4|5|6|7|8|9) is actually "15", which of course can never be equal to the last character. This should make sense, because 0|1|2|3|4|5|6|7|8|9 evaluates to 15 using integer math, which then gets converted to a String.

Alternatively, try this:

String code = "45m4av7";
char characterAtEnd = code.charAt(code.length() - 1);
System.out.println(characterAtEnd);

if(characterAtEnd < '0' || characterAtEnd > '9'){
    System.out.println("INVALID CRC CODE: last character in code in non-numerical.");
    System.exit(0);
}
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
  • is it possible to say "or" in an if-statement using integers? like if(whatever.equals(1 or 2 or 3 or 4 or 5)){ then do this; } Thank you so much for you help! – SmashAdams Jun 04 '13 at 01:42
  • 1
    The linguistic `||` is reserved for doing logical combinations of booleans. So you'd have to go `characterAtEnd.equals("0") || characterAtEnd.equals("1") || ...` – Prashant Kumar Jun 04 '13 at 02:11
  • That's why I go for the logistical-or approach. I create the set of all accepted ending characters, and check if the last character is in the acceptable set, and return false if it's anything else. It has the or-logic, without explicitly using or. – Prashant Kumar Jun 04 '13 at 02:13
  • I have edited my answer to approach the problem another way, by comparing ascii codes. Take a look – Prashant Kumar Jun 04 '13 at 02:26
0

You are doing bitwise operations here: if(!characterAtEnd.equals(String.valueOf(0|1|2|3|4|5|6|7|8|9)))

Check out the difference between | and ||

This bit of code should accomplish your task using regular expressions:

String code = "45m4av7";

if (!code.matches("^.+?\\d$")){
    System.out.println("INVALID CRC CODE");
}

Also, for reference, this method sometimes comes in handy in similar situations:

/* returns true if someString actually ends with the specified suffix */
someString.endsWith(suffix);

As .endswith(suffix) does not take regular expressions, if you wanted to go through all possible lower-case alphabet values, you'd need to do something like this:

/* ASCII approach */
String s = "hello";
boolean endsInLetter = false;
for (int i = 97; i <= 122; i++) {
    if (s.endsWith(String.valueOf(Character.toChars(i)))) {
        endsInLetter = true;
    }
}
System.out.println(endsInLetter);

/* String approach */
String alphabet = "abcdefghijklmnopqrstuvwxyz";
boolean endsInLetter2 = false;
for (int i = 0; i < alphabet.length(); i++) {
    if (s.endsWith(String.valueOf(alphabet.charAt(i)))) {
        endsInLetter2 = true;
    }
}
System.out.println(endsInLetter2);

Note that neither of the aforementioned approaches are a good idea - they are clunky and rather inefficient.

Going off of the ASCII approach, you could even do something like this:

ASCII reference : http://www.asciitable.com/

int i = (int)code.charAt(code.length() - 1);

/* Corresponding ASCII values to digits */
if(i <= 57 && i >= 48){
    System.out.println("Last char is a digit!");
}

If you want a one-liner, stick to regular expressions, for example:

System.out.println((!code.matches("^.+?\\d$")? "Invalid CRC Code" : "Valid CRC Code"));

I hope this helps!

ivan_m
  • 1
  • 1
  • Hi! Thanks for your response. I tried using the .endsWith method you mentioned, and it seems to have worked well. My only concern is that it seems as though i'd need to re-paste the line of code 26 times for each letter of the alphabet. Whereas I'd really like be able to say "if code ends with a or b or c or d...etc then print "invalid code". So I could have it all nice in one if statement. is that possible? also, I was wondering if for the .matches method, if it looks at what the items in the parenthesis are individually, or as a whole? – SmashAdams Jun 04 '13 at 01:36
  • Unfortunately endswith(suffix) does not take regular expressions (to my knowledge), and if you wanted to check for every letter of the alphabet, you could use a loop (although it would be clunky). As far as the .matches method is concerned, it looks at the last item to make sure that it is a decimal. I've modified my posting to help you out =) Good luck! – ivan_m Jun 04 '13 at 03:02