When the user enters their ID I want it to be in a specific format, they are mostly explained within the comments. I was wondering if their was an easier more efficient way of doing this. Also whether or not there is a way to change the entered letters to capital the way I've done the code, or any other method.
private boolean setCustomerID(String id) {
//Validates the customerID contains 3 letters a hypthen then 4 numbers
if ((id.charAt(0) < 'A' || id.charAt(0) > 'Z')
|| (id.charAt(1) < 'A' || id.charAt(1) > 'Z')
|| (id.charAt(2) < 'A' || id.charAt(2) > 'Z')
|| (id.charAt(3) != '-')
|| !isDigit(id.charAt(4))
|| !isDigit(id.charAt(5))
|| !isDigit(id.charAt(6))
|| !isDigit(id.charAt(7))) {
return false;
//Checks the user enters P, B or C for first letter
} else if ((id.charAt(0) == 'P' || id.charAt(0) == 'B' || id.charAt(0) == 'E')
//Checks the second and third letter are in the correct region
&& ((id.charAt(1) == 'S' && id.charAt(2) == 'C')
|| (id.charAt(1) == 'S' && id.charAt(2) == 'C')
|| (id.charAt(1) == 'W' && id.charAt(2) == 'A')
|| (id.charAt(1) == 'N' && id.charAt(2) == 'I')
|| (id.charAt(1) == 'N' && id.charAt(2) == 'E')
|| (id.charAt(1) == 'N' && id.charAt(2) == 'W')
|| (id.charAt(1) == 'M' && id.charAt(2) == 'I')
|| (id.charAt(1) == 'E' && id.charAt(2) == 'A')
|| (id.charAt(1) == 'S' && id.charAt(2) == 'E')
|| (id.charAt(1) == 'S' && id.charAt(2) == 'W'))){
// SC (Scotland), WA (Wales), NI (Northern Ireland), NE (North-East), NW (North-West),
//MI (Midlands), EA (East Anglia), SE (South-East), SW (South-West).
return true;
}
return false;
}
Use regex.
private boolean matchCustomerID(String id) {
return id.matches("^[PBE](?:SC|WA|NI|NE|NW|MI|EA|SE|SW)-\\d{4}\\b");
}
Regular expressions are one way of solving the problem. You can compose the pattern in a way that makes maintenance easier. Building on rcorreia's pattern, you can do something like:
private boolean setCustomerID(String id) {
char[] validFirstLetters = { 'P', 'B', 'E' };
String[] validRegions = { "SC", "WA", "NI", "NE", "NW", "MI", "EA", "SE", "SW" };
String pattern =
String.format("^[%s](?:%s)-\\d{4}$", new String(validFirstLetters),
String.join("|", validRegions));
return id.matches(pattern);
}
Note that this uses String.join() from Java 8. If you don't use Java 8 yet, consider using StringUtils from Apache Commons Lang.
Regexp is a great feature, but not easy to write and understand..
In this case, I would follow your way, but I would define some testing method. In this manner the code will be readable and easy to write Unit tests for it.
If you need some change later, you will understand the code.
Example:
testForLength();
testForLetters();
testForFirstTwoLetters();
Related
So, I'm a complete novice, and I've been having trouble with this for a while. I have to edit this file so that all the vowels and punctuation are gone, and so that the first and last sentence is capitalized, along with the first letter of every word. I've got the first half done, but the second part is giving me trouble.
Here's what I've got so far:
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;
public class SMcatcherInTheIO
{
public static void main (String [] args) throws Exception
{
File catcher= new File ("C:\\Users\\suvra\\Dropbox\\APCompSci 17-18\\AAA SM EXTRA CREDIT\\RyeCh1.txt");
BufferedReader br=new BufferedReader (new FileReader(catcher));
String rye;
while((rye=br.readLine()) !=null)
{
for (int i = 0; i < rye.length(); i++)
{
char c = rye.charAt(i);
if ((c == 'A') || (c == 'a') || (c == 'E') || (c == 'e') || (c == 'I') || (c == 'i') || (c == 'O') || (c == 'o') || (c == 'U') || (c == 'u')
|| (c == ',') || (c == '.') || (c == '?') || ( c == '!'))
{
String front = rye.substring(0, i);
String back = rye.substring(i + 1);
rye = front + "" + back;
}
}
System.out.println(rye);
}
br.close();
}}
Create two String objects :Last and First sentence. Than Loop trought you code and stop at the first point and add value to firstSentence. Do similar for the last sentence just stop at the point-1. After that:
String lastSentence=somethingalreadydoneabove;
String upperCase=lastSentence.toUpperCase();
Hope this would help you.
DK
This program is supposed to compare "DNA" strings.
Input:
3
ATGC
TACG
ATGC
CGTA
AGQ
TCF
First line represents how many times the program will be run. Each time it runs, it compares the two strings. A matches with T and vice versa. G matches with C and vise versa. So if the first letter of string 1 is A, the first letter of string 2 should be T. If the next one is T, the next one on the other string should be A and etc. If a letter other than A, T, G, or C appear, it is a bad sample. If its bad, print out bad, if its good, print out good. I tried many different combinations to this and they all worked fine but according the the judge's test data (they have different input), it failed. Does anyone see anything wrong with this? I know it might not be the most efficient way of getting the job done but it did, at least to my understanding.
Output:
GOOD
BAD
BAD
public class DNA
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner (new File ("dna.dat"));
int T = scan.nextInt();
scan.nextLine();
boolean valid = true;
for (int i = 0; i < T; i++)
{
String strand1 = scan.nextLine();
strand1 = strand1.toUpperCase();
String strand2 = scan.nextLine();
strand2 = strand2.toUpperCase();
for (int p = 0; p < strand1.length(); p++)
{
if (strand1.charAt(p) != 'A' && strand1.charAt(p) != 'T' && strand1.charAt(p) != 'G' && strand1.charAt(p) != 'C'
&& strand2.charAt(p) != 'A' && strand2.charAt(p) != 'T' && strand2.charAt(p) != 'G' && strand2.charAt(p) != 'C')
{
valid = false;
break;
}
if (strand1.length() != strand2.length())
{
valid = false;
break;
}
}
if (valid)
{
for (int p = 0; p < strand1.length(); p++)
{
if ((strand1.charAt(p) == 'A' && strand2.charAt(p) == 'T') || (strand1.charAt(p) == 'T' && strand2.charAt(p) == 'A')
|| (strand1.charAt(p) == 'G' && strand2.charAt(p) == 'C') || (strand1.charAt(p) == 'C' && strand2.charAt(p) == 'G'))
valid = true;
else
valid = false;
}
}
if (valid)
out.println("GOOD");
else
out.println("BAD");
valid = true;
}
}
}
I added the toUpperCase and compared the strings for equal length just as a last attempt to see if their data maybe had some lowercase letters or different length strings though they SHOULD all be the same length and uppercase. Nevertheless, the program was still rejected for "failing the judges test data."
You need a break in the second for loop when valid = false. For example if characters 1,2,3 are wrong but #4 is a match you will still end up with valid.
I would convert the strings to arrays to make things easier:
for (int i = 0; i < T; i++)
{
boolean valid = true;
String strand1 = scan.nextLine();
strand1 = strand1.toUpperCase();
String strand2 = scan.nextLine();
strand2 = strand2.toUpperCase();
if ( strand1.length() != strand2.length())
{
valid = false;
}
if (valid) {
char[] c1 = strand1.toCharArray();
char[] c2 = strand2.toCharArray();
for (int p = 0; p < c1.length; p++)
{
if (-1 == "ACTG".indexOf(c1[p]) || -1 == "ACTG".indexOf(c2[p]))
{
valid = false;
break;
}
}
if (valid)
{
for (int p = 0; p < c1.length; p++)
{
if (('A' == c1[p] && 'T' != c2[p]) ||
('T' == c1[p] && 'A' != c2[p]) ||
('C' == c1[p] && 'G' != c2[p]) ||
('G' == c1[p] && 'C' != c2[p])) {
valid = false;
break;
}
}
}
}
if (valid)
System.out.println("GOOD");
else
System.out.println("BAD");
}
Change all
&&
in
if (strand1.charAt(p) != 'A' && strand1.charAt(p) != 'T' && strand1.charAt(p) != 'G' && strand1.charAt(p) != 'C' && strand2.charAt(p) != 'A' && strand2.charAt(p) != 'T' && strand2.charAt(p) != 'G' && strand2.charAt(p) != 'C')
to
||
if ANY, not ALL character is other than A, T, G, or C, then we exit the loop.
I know there must be a simpler way to check, but this is what I'm doing right now.
if (g.charAt(0) == 'a' || g.charAt(0) =='b' || g.charAt(0) =='c' ||
g.charAt(0) == 'd' || g.charAt(0) =='e' || g.charAt(0) =='f' ||
g.charAt(0) == 'g' || g.charAt(0) =='h')
Relying on character ordering and that a..h is a consecutive range:
char firstChar = g.charAt(0);
if (firstChar >= 'a' && firstChar <= 'h') {
// ..
}
Use a regular expression for this one. Cut the first character of your String as a substring, and match on it.
if(g.substring(0, 1).matches("[a-h]") {
// logic
}
A variation on hemanth's answer:
if("abcdefgh".contains(g.substring(0,1))) do_something();
or
if("abcdefgh".indexOf(g.charAt(0)) >= 0) do_something();
Another way of doing it :
if(Array.asList("abcdefgh".toCharArray()).contains(g.charAt(0)))
{
//Logic
}
Currently I am checking a string for the following:
if(parseCommand.contains("vlan1") || parseCommand.contains("Fa0/1i")
|| parseCommand.contains("Fa0/1o") || parseCommand.contains("Fa1/0")
|| parseCommand.contains("Fa1/1") || parseCommand.contains("Fa1/2")
|| parseCommand.contains("Fa1/3") || parseCommand.contains("Fa1/4")
|| parseCommand.contains("Fa1/5") || parseCommand.contains("Fa1/6")
|| parseCommand.contains("Fa1/7") || parseCommand.contains("Fa1/8")
|| parseCommand.contains("Fa1/9") || parseCommand.contains("Fa1/11")
|| parseCommand.contains("Gi0")) {
//do things here
}
However it may contain vlan1 up to vlan4094 and i have to check for these. What is the simplest way to do this?
I have tried this just to match a vlan 1-9 folowed by 0-3 digits but it doesn't find anything:
if(parseCommand.matches(".*vlan[1-9](\\d){0,3}") || parseCommand.contains("Fa0/1i")
|| parseCommand.contains("Fa0/1o")|| parseCommand.contains("Fa1/0")
|| parseCommand.contains("Fa1/1") || parseCommand.contains("Fa1/2")
|| parseCommand.contains("Fa1/3") || parseCommand.contains("Fa1/4")
|| parseCommand.contains("Fa1/5") || parseCommand.contains("Fa1/6")
|| parseCommand.contains("Fa1/7") || parseCommand.contains("Fa1/8")
|| parseCommand.contains("Fa1/9") || parseCommand.contains("Fa1/11")
|| parseCommand.contains("Gi0")) {
Even if I try this nothing is found, why?
if(parseCommand.matches(".*vlan.*")
Use .matches("(?s).*vlan.*") or so for new line characters being catched by .; See DOTALL.
Please, can you give me a list of non printable characters in java programming? Thank you in advance.
Java uses the Unicode standard, so you should be asking about non-printable (non-printing?) characters in Unicode.
http://en.wikipedia.org/wiki/Unicode_control_characters
Java strings are unicode strings. Unicode doesn't have a concept of "non-printable" characters, exactly, but the ASCII non-printable range, along with several other characters, are considered Unicode control characters.
Are spaces printable? What about the private use area? Please modify the code to your definition of "printable" :)
import static java.lang.Character.*;
for (int i=0; i<MAX_CODE_POINT; i++) {
int t = getType(i);
boolean p = t == CONTROL || t == CONNECTOR_PUNCTUATION || t == CURRENCY_SYMBOL || t == DASH_PUNCTUATION || t == DECIMAL_DIGIT_NUMBER || t == ENCLOSING_MARK || t == END_PUNCTUATION || t == FINAL_QUOTE_PUNCTUATION || t == INITIAL_QUOTE_PUNCTUATION || t == LETTER_NUMBER || t == LOWERCASE_LETTER || t == MATH_SYMBOL || t == MODIFIER_LETTER || t == MODIFIER_SYMBOL || t == OTHER_LETTER || t == OTHER_NUMBER || t == OTHER_PUNCTUATION || t == OTHER_SYMBOL || t == START_PUNCTUATION || t == TITLECASE_LETTER || t == UPPERCASE_LETTER;
if (!p) {
System.out.println("Non printable codepoint " + i);
}
}