I have clumsy method with multiple ugly conditions statements. For Example:
public String getKafkaTopicName(EvntMsg evntMsg) {
Table table = fillerTableSection(evntMsg);
String[] msgDetails = table.getMessageString().split("\\^");
String topicName;
if (("QUEUE_R".equals(table.getDeliveryChannel()) && "A".equals(msgDetails[0])) ||
("M".equals(table.getDeliveryChannel()) && "F".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_W".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_SRC_MESS".equals(table.getCode())) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_TGT_MESS".equals(table.getCode()))) {
topicName = paramsHelper.getKafkaTopicNameBalance();
} else if (("SMS3DS_CHECK".equals(table.getDeliveryChannel())) ||
("SMS3DS".equals(table.getDeliveryChannel()))) {
topicName = paramsHelper.getKafkaTopicNameOtp();
} else if (("SBRF_I".equals(table.getDeliveryChannel()) && "I".equals(msgDetails[0])) ||
("SBRF_B".equals(table.getDeliveryChannel()) && "B".equals(msgDetails[0])) ) {
topicName = paramsHelper.getKafkaTopicNameIssuing();
} else if (("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "C0".equals(table.getCode().substring(0, 2))) ||
("QUEUE_R".equals(table.getDeliveryChannel()) && "AP".equals(msgDetails[0])) ||
("QUEUE_D".equals(table.getDeliveryChannel()) && "AP".equals(msgDetails[0]))) {
topicName = paramsHelper.getKafkaTopicNameNotify();
} else {
topicName = paramsHelper.getKafkaTopicNameUndefined();
}
return topicName;
}
How it can be modified to improve readability?
What you can do is isolating in methods that return boolean
boolean test0(Table table, String[] msgDetails){
return "QUEUE_R".equals(table.getDeliveryChannel()) && "A".equals(msgDetails[0])) ||
("M".equals(table.getDeliveryChannel()) && "F".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_W".equals(table.getDeliveryChannel()) && "S".equals(msgDetails[0])) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_SRC_MESS".equals(table.getCode())) ||
("QUEUE_Q".equals(table.getDeliveryChannel()) && "C".equals(msgDetails[0]) && "DD_TGT_MESS".equals(table.getCode()))
}
Related
The phone numbers that I will check have two formats:
(555) 555-5555 and 555-555-5555
However, my if condition keeps failing in the code below. I am trying to check if the str.charAt(0) is a ( or a number
public static boolean isPhoneNumber(String str) {
if (str.contains("-")) {
for (int i = 0; i < str.length(); i++) {
if ((i == 0 && str.charAt(0) != '(')
|| (i == 0 && Character.isDigit(str.charAt(0)) == false)) {
System.out.println(Character.isDigit(str.charAt(0)));
return false;
}
if (i == 4 && str.charAt(4) != ')' || i == 4 && !(Character.isDigit(str.charAt(4)))) {
System.out.println("here2");
return false;
}
if (i == 3 && str.charAt(3) != '-'
|| i == 3 && (Character.isDigit(str.charAt(3)) == false)) {
System.out.println(str.charAt(3));
return false;
}
if (i == 5 && str.charAt(5) != ' ' || i == 5 && !(Character.isDigit(str.charAt(5)))) {
System.out.println("here4");
return false;
}
if (i == 7 && str.charAt(i) != '-' || i == 7 && !(Character.isDigit(str.charAt(i)))) {
System.out.println("here5");
return false;
}
if (i == 9 && str.charAt(9) != '-' || i == 9 && !(Character.isDigit(str.charAt(9)))) {
System.out.println("here6");
return false;
}
if (i != 0 && i != 3 && i != 5 && i != 7 && i != 9 && !(Character.isDigit(str.charAt(i)))) {
System.out.println("here7");
return false;
}
}
return true;
} else {
return false;
}
}
A general regex pattern which might work here is:
^(?:\(\d{3}\)\s*|\d{3}-)\d{3}-\d{4}$
Here is an explanation of the above regex:
^ from the start of the input
(?:
\(\d{3}\) match (xxx)
\s* followed by optional whitespace
| OR
\d{3}- xxx-
)
\d{3}-\d{4} match xxx-xxxx
$ end of the input
Demo
This would cover both versions of the phone number you gave above. In Java, we can use String#matches here:
String phone1 = "(555) 555-5555";
String phone2 = "555-555-5555";
if (phone1.matches("(?:\\(\\d{3}\\)\\s*|\\d{3}-)\\d{3}-\\d{4}")) {
System.out.println(phone1 + " is in a valid format");
}
if (phone2.matches("(?:\\(\\d{3}\\)\\s*|\\d{3}-)\\d{3}-\\d{4}")) {
System.out.println(phone2 + " is in a valid format");
}
How can I reduce the number of operators to 3 properly,
with multiple methods ? or loop ?
the constant of Actions is Int
String act = req.getParameter("ACTION");
int actInt = -1;
try {
actInt = Integer.parseInt(act);
} catch (NumberFormatException nfe) {
//
}
boolean actionNulle = act == null;
boolean actionDefaut = actionNulle || actInt == -1;
if ( (actionReq.equals(ACTION_LIST_ENREG) && (actionDefaut || !(actInt == Actions.AJOUTER || actInt == Actions.VALIDER || actInt == Actions.NOUVEAU || actInt == Actions.NOUVEAU_PAR_COPIE)))
|| (actionReq.equals("PreAbattage") && (actionDefaut || (actInt == Actions.DEFAUT)))
|| (actionReq.equals(ACTION_FORMULAIRE_RECHERCHE) && (actionDefaut || !(actInt == Actions.NOUVEAU_PAR_COPIE || actInt == Actions.NOUVEAU)))
|| (actionReq.equals("ModificationMultiple") && (actionDefaut || !(actInt == Actions.OUI || actInt == Actions.SUBSTITUTION)))
|| (actionReq.equals(ACTION_VISU_RECORD) && (actionDefaut || !(actInt == Actions.COPIE_PRIVE || actInt == Actions.NOUVEAU_PRIVE || actInt == Actions.MODIFIER || actInt == Actions.RESULTAT_CREATION)))
|| (actionReq.equals("VisuLock") && (actionDefaut || !(actInt == Actions.DETRUIRE_VERROU)))
|| (actionReq.equals("CopiePublique") && (actionDefaut || !(actInt == Actions.CREER_DONNEES_PUBLIQUES)))
|| (actionReq.equals("VisuSessions") && (actionDefaut || !(actInt == Actions.DETRUIRE_SESSION)))
|| (actionReq.equals(ACTION_VISU_MESSAGE) && (actionDefaut))
|| (actionReq.equals("RecapModifPub") && (actionDefaut || actInt == Actions.VALIDER))
) {
return true;
}
You could use a Map<String, Set<Integer>>
private final Map<String, Set<Integer>> validCombinations = initializeMap();
// initialize the map once
// later, in your code:
return validCombinations.containsKey(actionReq) &&
(actionDefault || validCombinations.get(actionReq).contains(actInt));
This is my first question here so I'm sorry for any mistake. I am writing a tic-tac-toe game. I have problem with this.
if (!table[0][0].equals(" ") && !table[1][0].equals(" ") && !table[2][0].equals(" ") &&
!table[0][1].equals(" ") && !table[1][1].equals(" ") && !table[2][1].equals(" ") &&
!table[0][2].equals(" ") && !table[1][2].equals(" ") && !table[2][2].equals(" ")) {
System.out.println("Draw!");
return true;
}
For example: if I insert any String in table[0][0], it returns true despite other fields being " ".
I have initialized table in Board class. This is the constructor:
public Board() {
sc = new Scanner(System.in);
board = new Mark[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = new Mark(" ");
}
}
}
This is my checkResult method:
public boolean checkResult() {
if ((table[0][0] == table[1][0] && table[1][0] == table[2][0] && !table[2][0].equals(" "))
|| (table[0][1] == table[1][1] && table[1][1] == table[2][1] && !table[2][1].equals(" "))
|| (table[0][2] == table[1][2] && table[1][2] == table[2][2] && !table[2][2].equals(" "))
|| (table[0][0] == table[0][1] && table[0][1] == table[0][2] && !table[0][2].equals(" "))
|| (table[1][0] == table[1][1] && table[1][1] == table[1][2] && !table[1][2].equals(" "))
|| (table[2][0] == table[2][1] && table[2][1] == table[2][2] && !table[2][2].equals(" "))
|| (table[0][0] == table[1][1] && table[1][1] == table[2][2] && !table[2][2].equals(" "))
|| (table[0][2] == table[1][1] && table[1][1] == table[2][0] && !table[2][0].equals(" "))) {
System.out.println("You won");
return true;
}
if (!table[0][0].equals(" ") && !table[1][0].equals(" ") && !table[2][0].equals(" ") && !table[0][1].equals(" ")
&& !table[1][1].equals(" ") && !table[2][1].equals(" ") && !table[0][2].equals(" ")
&& !table[1][2].equals(" ") && !table[2][2].equals(" ")) {
System.out.println("Draw!");
return true;
}
return false;
}
And this is my Test class:
public class Test {
public static void main(String[] args) {
Logika lg = new Logika();
do{
lg.insertMark2();
lg.printBoard();
} while(!(lg.checkResult()));
}
}
When I do my first insert, program stops.
I can't get why my code catches exception..
I assume there is something wrong with a constructor, but I don't see where exactly..
public class OrderDate
{
private String date;
public OrderDate(String date) throws IllegalDateFormatException
{
IllegalDateFormatException wrongDate =
new IllegalDateFormatException("Date must have the following"
+ " format: dd/mm/yy");
if(date.length() > 8
|| (date.charAt(0) == 0 && date.charAt(1) == 0)
|| (date.charAt(3) == 0 && date.charAt(4) == 0)
|| (date.charAt(0) == 3 && date.charAt(1) > 1)
|| (date.charAt(3) == 1 && date.charAt(4) > 2)
|| date.charAt(2) != '/'
|| date.charAt(5) != '/'
|| date.charAt(0) > 3
|| date.charAt(3) > 1
|| !isDigit(date.charAt(0))
|| !isDigit(date.charAt(1))
|| !isDigit(date.charAt(3))
|| !isDigit(date.charAt(4))
|| !isDigit(date.charAt(6))
|| !isDigit(date.charAt(7)))
throw wrongDate;
else
this.date = date;
}
private boolean isDigit(char z)
{
return z >= '0' && z <= '9';
}
}
In the main method I use the following:
try
{
OrderDate myDate = new OrderDate("10/02/15");
System.out.println("all fine");
}
catch(Exception e)
{
System.out.println(e);
System.exit(0);
}
And the exception class:
public class IllegalDateFormatException extends Exception
{
public IllegalDateFormatException(String error)
{
super(error);
}
}
Thanks very much for help!
The reason you are throwing an exception is your large if/else block has a bug in it.
(date.charAt(0) == 0 && date.charAt(1) == 0)
|| (date.charAt(3) == 0 && date.charAt(4) == 0)
|| (date.charAt(0) == 3 && date.charAt(1) > 1)
|| (date.charAt(3) == 1 && date.charAt(4) > 2)
...
|| date.charAt(0) > 3
|| date.charAt(3) > 1
string.charAt(in) returns a character. you are checking to see if the values are ints. Since characters can be represented as integer values (like ASCII value etc) expressions like date.charAt(1) > 1 will always be true since printable characters start as ASCII value 32
Change it to single quoted values
(date.charAt(0) == '0' && date.charAt(1) == '0')
|| (date.charAt(3) == '0' && date.charAt(4) == '0')
|| (date.charAt(0) == '3' && date.charAt(1) > '1')
|| (date.charAt(3) == '1' && date.charAt(4) > '2')
...
|| date.charAt(0) > '3'
|| date.charAt(3) > '1'
On an unreleated note, it's not a good idea to create the Exception object before checking for the condition. Exception creation can be expensive so don't waste the CPU time unless you need to throw it. This is better:
if(date.length() > 8 ...){
throw new IllegalDateFormatException("Date must have the following"
+ " format: dd/mm/yy");
}
this.date=date;
Using Simpledateformat may make your life a lot easier.
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
try {
Date date = simpleDateFormat.parse("15/02/99");
} catch (ParseException e) {
throw IllegalDateFormat("Date must have the following format: " +
"dd/mm/yy");
}
charAt(someIndex)
returns a char not an int. You have to check for
== '0'
need to get the card issuer along with the number, only outputs "Unknown".
(the bottom is just tester code).
am i trying to test the wrong variable, am i incorrectly using indexOf()? please, any help would be appreciated
public class CreditCard
{
private String card_number;
private boolean is_number;
private String number_string = "";
public String issuer_name = "";
public CreditCard(String card_number)
{
this.card_number = card_number;
}
public String toString()
{
for (int x = 0; x < card_number.length(); x++)
{
char y = card_number.charAt(x);
is_number = Character.isDigit(y);
if (is_number)
{
number_string += y;
}
}
String s = number_string + " was issued by " + getIssuer();
return s;
}
public void setIssuer(String issuer)
{
issuer_name = issuer;
}
public String getIssuer()
{
String issuer_Name;
if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16)
{
issuer_Name = "VISA";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('8') == 1 && card_number.length() == 14)
{
issuer_Name = "Diner's Club";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('4') == 1 && card_number.length() == 15)
{
issuer_Name = "American Express";
}
if (card_number.indexOf('3') == 0 && card_number.indexOf('7') == 1 && card_number.length() == 15)
{
issuer_Name = "American Express";
}
if (card_number.indexOf('5') == 0 && card_number.indexOf('1') == 1 && card_number.length() == 16)
{
issuer_Name = "MasterCard";
}
if (card_number.indexOf('5') == 0 && card_number.indexOf('5') == 1 && card_number.length() == 16)
{
issuer_Name = "MasterCard";
}
if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
{
issuer_Name = "Discover";
}
else
{
issuer_Name = "Unknown";
}
return issuer_Name;
}
public static void main(String[] args)
{
System.out.println(new CreditCard("42225-22222222"));
System.out.println(new CreditCard("76009644571"));
System.out.println(new CreditCard("50197170-10103742"));
System.out.println(new CreditCard("6331101899890016"));
}
}
The if else clause would start something like
if (card_number.indexOf('4') == 0 && card_number.length() == 13 || card_number.length() == 16) {
issuer_Name = "VISA";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('0') == 2 && number_string.length() == 14) {
issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('5') == 2 && number_string.length() == 14) {
issuer_Name = "Diner's Club";
} else if (card_number.indexOf('3') == 0 && card_number.indexOf('6') == 1 && card_number.length() == 14) {
issuer_Name = "Diner's Club";
} else {
issuer_Name = "Other";
}
Note I am not looking at the correctness of the algorithm, but you asked for how to do the if/else
if (card_number.indexOf('6') == 0 && card_number.indexOf('0') == 1 && card_number.indexOf('1') == 2 && number_string.indexOf('1') == 3 && number_string.length() == 16)
{
issuer_Name = "Discover";
}
else
{
issuer_Name = "Unknown";
}
Check this Code.
If a card issuer is not Discover, the output ALWAYS "Unknow"
if (true) {
n=1;
}
if (true) {
n=2;
}
if (true) {
n=3;
}
else {
n=0;
}
The n is either 3 or 0, cannot be 1 or 2.
Because the last if-else will override the n
You can fix this issue by changing "if" to "else if"