• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java character checking (regex? Range object?)

Yossarian22

[H]ard|Gawd
Joined
Apr 27, 2009
Messages
1,799
In Java, I almost have this assignment done but I'm stuck on a logical condition.

I built this entire class from scratch and the UML diagram dictates that the employee number is a string in the format XXX-L, where each X is a digit within the range of 0-9 and L is the letter within the range of A-M.

I have checking for the digits and the hyphen complete, however, is there a way to check the 4th element of this string and ensure it is within the unicode range of A-M besides having 13 logical conditionals (if String.CharAt(4) == 'A' && String.CharAt(4) == 'B' ... etc) ?

Thank you in advance.
 
Check the ASCII value of the character at that position. You would need chars from 65 to 77 inclusive for an uppercase match. Add another range for lowercase match which would be 97 to 109.
 
But characters in Java are 2 byte unicode characters, right? :confused:

Are the uppercase and lowercase characters still sequential in regards to integer value?
A-Z and a-z?
 
are you allowed to use regexes? that would make it trivial but i suspect it might somewhat defeat the point.

anyway, you can compare characters directly such as
if ('A' <= someChar) { ... }
which works like you might expect. combining a couple of checks you can check for a range, and add in an or check against say upper and lowercase
 
are you allowed to use regexes? that would make it trivial but i suspect it might somewhat defeat the point.

anyway, you can compare characters directly such as
if ('A' <= someChar) { ... }
which works like you might expect. combining a couple of checks you can check for a range, and add in an or check against say upper and lowercase

In other words, the same ol' way I'd do it in a language without fancy regular expressions:

Code:
(name.charAt(4) >= 'A' && name.charAt(4) <= 'M')

I wasn't sure if Java would allow you to use such a method of doing that (I'm more used to C++, took Advanced Java at my college for some reason? Not sure why...)

What would the solution to this problem look like with a regular expression? We haven't covered them and this is a very lax course, so I don't expect we'll touch on them. Ever.
 
In other words, the same ol' way I'd do it in a language without fancy regular expressions:

Code:
(name.charAt(4) >= 'A' && name.charAt(4) <= 'M')

I wasn't sure if Java would allow you to use such a method of doing that (I'm more used to C++, took Advanced Java at my college for some reason? Not sure why...)

What would the solution to this problem look like with a regular expression? We haven't covered them and this is a very lax course, so I don't expect we'll touch on them. Ever.
pretty much, just didn't want to you know, type it out since it's technically homework even though it's pretty simple

basic regex matching in java is pretty simple if you understand regexes already
Code:
if (str.matches("^\\d{3}-[A-Ma-m]$")) {...}

if you don't understand regexes already, i'll refer you to google because it's rather complicated. took me quite some time before i got comfortable with them.
 
For such a trivial pattern, regexes are an inappropriate solution.
 
pshaw... it's just a little overhead for a lot less code...
 
Apparently, it's more overhead than you realize.
 
Here's one way to do this in Java using regular expressions:

Code:
Pattern regex = Pattern.compile("([0-9]{3})\\-([A-M])");
    Matcher m = regex.matcher("134-A"); //for example
        
    if(m.matches()){      
      for(int i = 1; i <= m.groupCount(); i++){
        System.out.println(i + " : " + m.group(i));
      }
    }

This example goes further than matching by also extracting the relevant fields: group 1 contains the digits prior to the dash, group 2 contains the following letter code. It's really not that much code. The benefit to using a reg-ex over other String parsing logic is that if you need to make a change to the parsing, you usually only need to modify the pattern. If you do the parsing manually, you might have to dig deeper into whatever parsing logic you're using.

The downside, of course, is needing to know something about reg-ex syntax. If you learn, I can almost guarantee you will find it useful down the line if you continue to do computer-based work.

forgot to mention: the Pattern & Matcher classes come from the java.util.regex package; be sure to import this if you want to try out the example.
 
Back
Top