简体   繁体   中英

Comparing String Integers Issue

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?

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

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!

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