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.
Related
I need help with one if condition in java code
public class Main
{
public static void main(String[] args) {
String sHeaderStatus = "1";
Boolean hasButton = false;
Boolean editableLineStatus =true;
String sFrom = "REQ";
int canChangeSupplier = 0;
if ((sHeaderStatus.equals("1") || canChangeSupplier == 1 &&
(sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
|| hasButton && editableLineStatus && !sHeaderStatus.equals("85")) || sFrom.equals("APPROVAL")) {
String valdiaton ="true11";
System.out.println(valdiaton);
}
}
result is true11
public class Main
{
public static void main(String[] args) {
String sHeaderStatus = "1";
Boolean hasButton = false;
Boolean editableLineStatus =false; //changed this one to false
String sFrom = "REQ";
int canChangeSupplier = 0;
if ((sHeaderStatus.equals("1") || canChangeSupplier == 1 &&
(sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
|| hasButton && editableLineStatus && !sHeaderStatus.equals("85")) || sFrom.equals("APPROVAL")) {
String valdiaton ="true11";
System.out.println(valdiaton);
}
}
result is still true11
I am not able to understood the issue.
Per my understanding...
sHeaderStatus.equals("1") || canChangeSupplier == 1 // gave true
(sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
|| hasButton // gave false
become true && false && true && true
Similarly second code would become true && false && false &&true
Am not sure how this become true and below line printed.
&& happens before ||
Write the code like this
if (
(
sHeaderStatus.equals("1")
|| canChangeSupplier == 1 && (sHeaderStatus.equals("10") || sHeaderStatus.equals("14") || sHeaderStatus.equals("85") || sHeaderStatus.equals("86") || sHeaderStatus.equals("87"))
|| hasButton && editableLineStatus && !sHeaderStatus.equals("85")
)
|| sFrom.equals("APPROVAL")
)
You can see that you always will have (something) || false with the values given.
And changing only editableLineStatus will modify only the operator "grouping" for hasButton && editableLineStatus && !sHeaderStatus.equals("85").
However, regardless of what you change that to, you have sHeaderStatus.equals("1"), which is true, resulting in logic of
(true || (false && false) || (false && true/false && true)) || false
which, in total, is true, therefore entering the conditional
I have a file in .dat format. Here is a sample
||= (N ) =|| 1|| 0.938 || --- || 0.5 || (****)|| 0.5 || 0 || 0 || 0 || 0.700 || (p)=2212, (n)=2112 ||
||= (\Delta ) =|| 2|| 1.232 || 0.118 || 1.5 || (****)|| 1.5 || 0 || 0 || 3 || 1.076 || (\Delta^{++})=2224, (\Delta^+)=2214, (\Delta^0)=2114, (\Delta^-)=1114 ||
||= (P_{11}(1440) ) =|| 3|| 1.462 || 0.391 || 0.5 || (****)|| 0.5 || 0 || 0 || 3 || 1.076 || 202212, 202112 ||
||= (S_{11}(1535) ) =|| 4|| 1.534 || 0.151 || 0.5 || ( ***)|| 0.5 || 0 || 0 || 3 || 1.076 || 102212, 102112 ||
I am trying to use Scanner to read this file and delimit the line by the "||" and then send into and ArrayList for future processing. Here is a sample of my code where I use the delimiter
String file = "data.dat";
Scanner s = null;
try {
s = new Scanner(new File(file)).useDelimiter("\\|\\|"); //here is the use of my delimiter
ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()) { //notice I am using hasNextLine because each line must be unique to create the HashMap
list.add(s.nextLine());
}
s.close();
for (String string : list) { //Lets print out the values of the list
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But my output still has values of the delimiter, i.e. here is the output:
||= (N ) =|| 1|| 0.938 || --- || 0.5 || (****)|| 0.5 || 0 || 0 || 0 || 0.700 || (p)=2212, (n)=2112 ||
||= (\Delta ) =|| 2|| 1.232 || 0.118 || 1.5 || (****)|| 1.5 || 0 || 0 || 3 || 1.076 || (\Delta^{++})=2224, (\Delta^+)=2214, (\Delta^0)=2114, (\Delta^-)=1114 ||
||= (P_{11}(1440) ) =|| 3|| 1.462 || 0.391 || 0.5 || (****)|| 0.5 || 0 || 0 || 3 || 1.076 || 202212, 202112 ||
||= (S_{11}(1535) ) =|| 4|| 1.534 || 0.151 || 0.5 || ( ***)|| 0.5 || 0 || 0 || 3 || 1.076 || 102212, 102112 ||
I have searched and found no answer that helped. I am also seeing a warning about a "Resource leak: '' is never closed" with the line
s = new Scanner(new File(file)).useDelimiter("\\|\\|");
which disappears if the line is broken into
s = new Scanner(new File(file));
s.useDelimiter("\\|\\|");
Any help is appreciated.
You don't need to use backslash escape. instead of \\|\\| as your delimiter, just split the string after it's read from the file.
String file = "data.dat";
Scanner s = null;
try {
s = new Scanner(new File(file)); //no more delimiter. It's not needed
ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()) {
String[] strings = s.nextLine().split("[||]");
for (int i = 0; i < strings.length; i++) {
list.add(strings[i]);
}
}
s.close();
for (String string : list) { //Lets print out the values of the list
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This should fix it.
You can just read the line without delimiter, and then replace || with whatever you want using replace method.
Code
public static void main (String[] args) throws java.lang.Exception{
Scanner s = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()) {
String newLine = s.nextLine().replace("||","");
list.add(newLine);
// or just like this
//list.add(s.nextLine().replace("||",""));
}
s.close();
for (String string : list) {
System.out.println(string);
}
}
Check the output here.
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();
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
}
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);
}
}