I have an Excel file, which I am reading using Apache POI APIs; which gives me control over cells/columns in my Excel.
I have around 17 columns in my Excel and these are with different datatypes. I want to populate a model object from these column values. So, I came up with something like below to populate my model object:
private WHTApps populateModel(int columnIndex, Object cellValue) {
WHTApps whtApp = new WHTApps();
switch (columnIndex) {
case 0:
whtApp.setVendorCode((int) cellValue);
break;
case 1:
whtApp.setVendorName((String) cellValue);
break;
case 2:
whtApp.setCountry((String) cellValue);
break;
case 3:
whtApp.setApplicationReceivedFromUSDate((Date) cellValue);
break;
case 4:
whtApp.setContractNumber((String) cellValue);
break;
case 5:
whtApp.setContractEffectiveDate((Date) cellValue);
break;
case 6:
whtApp.setContractExpirationDate((Date) cellValue);
break;
case 7:
whtApp.setAutomaticRenewal((String) cellValue);
break;
case 8:
whtApp.setReducedRate((int) cellValue);
break;
case 9:
whtApp.setForm17((String) cellValue);
break;
case 10:
whtApp.setResidencyCertIssueDate((Date) cellValue);
break;
case 11:
whtApp.setTaxAuthoritySubmissionDate((Date) cellValue);
break;
case 12:
whtApp.setTaxAuthorityAcceptanceDate((Date) cellValue);
break;
case 13:
whtApp.setStatus((String) cellValue);
break;
case 14:
whtApp.setForm17Expiration((Date) cellValue);
break;
case 15:
whtApp.setComments((String) cellValue);
break;
case 16:
whtApp.setRemarks((String) cellValue);
break;
case 17:
whtApp.setCategory((String) cellValue);
break;
default:
}
return whtApp;
}
But, this gives rise to a really high cyclomatic complexity value of 19.
Now how do I reduce the cyclomatic complexity as I have 17 columns which I need to populate in my model object?
Thanks for helping me out
Akshay
Related
Can someone explain me why my loop is going from 0 to 7? I need to get only in the first 3 cases, but the program is going through all.. The code is in the show() method.
for(brojac=0; brojac<3; brojac++){
switch(brojac){
case 1:
figura1.setPosition(pomx[random], pomy[random]);
stage.addActor(figura1);
System.out.println("1");
case 2:
figura2.setPosition(pomx[random], pomy[random]);
stage.addActor(figura2);
System.out.println("2");
case 3:
figura3.setPosition(pomx[random], pomy[random]);
stage.addActor(figura3);
System.out.println("3");
case 4:
figura4.setPosition(pomx[random], pomy[random]);
stage.addActor(figura4);
System.out.println("4");
case 5:
figura5.setPosition(pomx[random], pomy[random]);
stage.addActor(figura5);
System.out.println("5");
case 6:
figura6.setPosition(pomx[random], pomy[random]);
stage.addActor(figura6);
System.out.println("6");
case 7:
figura7.setPosition(pomx[random], pomy[random]);
stage.addActor(figura7);
System.out.println("7");
}
}
Because the break is missing. After every case you need the break keyword. For a detailed explanation see switch statement java tutorial
e.g.
for(brojac=0; brojac<3; brojac++){
switch(brojac){
case 1:
figura1.setPosition(pomx[random], pomy[random]);
stage.addActor(figura1);
System.out.println("1");
break; /// Break here
case 2:
figura2.setPosition(pomx[random], pomy[random]);
stage.addActor(figura2);
System.out.println("2");
break; /// Break here
case 3:
figura3.setPosition(pomx[random], pomy[random]);
stage.addActor(figura3);
System.out.println("3");
break; /// Break here
case 4:
figura4.setPosition(pomx[random], pomy[random]);
stage.addActor(figura4);
System.out.println("4");
break; /// Break here
case 5:
figura5.setPosition(pomx[random], pomy[random]);
stage.addActor(figura5);
System.out.println("5");
break; /// Break here
case 6:
figura6.setPosition(pomx[random], pomy[random]);
stage.addActor(figura6);
System.out.println("6");
break; /// Break here
case 7:
figura7.setPosition(pomx[random], pomy[random]);
stage.addActor(figura7);
System.out.println("7");
break; /// Break here
}
}
Otherwise the other cases will be executed if one of the above cases gets evaluated to true.
That's how Java's switch statements work.
They "fall through" the cases once one of them matches. You need to add break; after all your cases.
for(brojac=0; brojac<3; brojac++){
switch(brojac){
case 1:
figura1.setPosition(pomx[random], pomy[random]);
stage.addActor(figura1);
System.out.println("1");
break;
case 2:
figura2.setPosition(pomx[random], pomy[random]);
stage.addActor(figura2);
System.out.println("2");
break;
case 3:
figura3.setPosition(pomx[random], pomy[random]);
stage.addActor(figura3);
System.out.println("3");
break;
case 4:
figura4.setPosition(pomx[random], pomy[random]);
stage.addActor(figura4);
System.out.println("4");
break;
case 5:
figura5.setPosition(pomx[random], pomy[random]);
stage.addActor(figura5);
System.out.println("5");
break;
case 6:
figura6.setPosition(pomx[random], pomy[random]);
stage.addActor(figura6);
System.out.println("6");
break;
case 7:
figura7.setPosition(pomx[random], pomy[random]);
stage.addActor(figura7);
System.out.println("7");
break;
}
}
I’m using switch in my code and i wanna get case value in every case block. how can i get it?
switch(value)
{
case Value1:
function1(Value1);
// i want something like this : function1(getCaseValue)
break;
case Value2:
function1(Value2);
break;
case Value3:
function2(Value3);
break;
}
Inside of your case statements, you can call function with value variable function1(value). Here is full code:
switch(value) {
case Value1:
function1(value);
break;
case Value2:
function1(value);
break;
case Value3:
function2(value);
break;
}
If you will always call same function in specific set of values, you can even make your switch-case statements simpler:
switch(value) {
case Value1:
case Value2:
function1(value);
break;
case Value3:
function2(value);
break;
}
If you have the same handling for different cases, instead of duplicating the handling, you can write clearer, simpler code by combining them like this:
switch (value) {
case 0:
case 1:
case 2:
function1(value);
break;
}
If you want to add additional handling for other cases, add them, too:
case 3:
function2(value);
break;
and maybe inlude a default:
default:
function3(value);
private void Button_CalculateActionPerformed(java.awt.event.ActionEvent evt) {
try{ a=Double.parseDouble(TextField_First.getText());
b=Double.parseDouble(TextField_Second.getText());
switch(operation){
case 1:result=a+b;
break;
case 2:result=a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
break;
case 5:result=a%b;
break;
case 6:result=Math.pow(a,b);
break;
case 7:
TextField_Second.setText(""+a);
result=Math.sqrt(a);
TextField_Second.setText(""+a);
break;
case 8:result=Math.abs(a);
default:System.out.println("Please select an operation");
}
TextField_Result.setText(""+result);
}catch(NumberFormatException e){Label_error.setText("Please use your
common sense ;-;");}
}
What I'm trying to do is set the value (and text) of TextField_Second to 0.0, if TextField_Second is null/has no value. So if TextField_First's value is 9, I get the square root, I get an error (NumberFormatException), but I used try and catch, so if that does happen, a label will appear saying, "Please use your common sense ;-;".
P.S. It only works if TextField_Second has a value, and it doesn't affect the answer.
Try something like this;
Note: you have not add break statement to case 8:
try
{
if (!TextField_First.getText().equals(""))
{
if(TextField_Second.getText().equals(""))
{
TextField_Second.setText("0.0");
}
a=Double.parseDouble(TextField_First.getText());
b=Double.parseDouble(TextField_Second.getText());
switch(operation){
case 1:result=a+b;
break;
case 2:result=a-b;
break;
case 3:result=a*b;
break;
case 4:result=a/b;
break;
case 5:result=a%b;
break;
case 6:result=Math.pow(a,b);
break;
case 7:
TextField_Second.setText(""+a);
result=Math.sqrt(a);
TextField_Second.setText(""+a);
break;
case 8:result=Math.abs(a);
break;
default:System.out.println("Please select an operation");
break;
}
}
This question already has answers here:
Using switch statement with a range of value in each case?
(20 answers)
Closed 7 years ago.
Here is my code:
switch(age) {
case 10:
System.out.println("You are too young to drive.");
break;
case 20:
System.out.println("You can drive!");
break;
default:
System.out.println("Error");
}
What happens if the age is 15? Well, it gives me an error. So I was wondering if it's possible to include a condition within the case. For example,
case (age>=10 && age<=20):
System.out.println("You're still too young to drive...");
break;
I could use an if statement, but I'm wondering if this is possible with a switch.
case Requires a Constant Expression
No. It's not possible because a case must be a constant expression. But you could (as you guessed) use an if. Logically, anyone under 20 (assuming that's the legal age) is too young to drive.
final int drivingAge = 20;
if (age < drivingAge) {
System.out.printf("%d is too young to drive...%n", age);
} else {
System.out.printf("%d is old enough to drive.%n", age);
}
Alternatively
That could be written with ? : conditional operator (aka a ternary expression; note some people find the ternary difficult to read) like
final int drivingAge = 20;
System.out.printf(age < drivingAge ? "%d is too young to drive...%n"
: "%d is old enough to drive.%n", age);
Because of fall-through you can do this:
switch(age){
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
System.out.println("You are too young to drive.");
break;
case 20:
System.out.println("You can drive!");
break;
default:
System.out.println("Error");
}
It is a little more code but it accomplishes the same thing.
No, in Java you cannot do it. Use if statement instead. Constructs similar to what you want exist in other languages like Scala.
You can't do this in Java. A switch jumps to the case that matches the value you're switching on. You can't use expressions of age inside the case. However, you can use something called a "fallthrough" (see here and search for "fall through").
So, for your example, you could do something like:
switch(age)
{
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
System.out.println("You are too young to drive.");
break;
case 20:
case 21:
System.out.println("You can drive!");
break;
default:
System.out.println("Error");
}
However, your problem is best solved with an if.
Sadly, it's not possible in Java. You'll have to resort to using if-else statements.You can try this, which is very repetitive.
switch(age){
case 10: case 11: case 12: case 13: case 14: case 15:
//And So on
System.out.println("You are too young to drive.");
break;
case 20:
System.out.println("You can drive!");
break;
default:
System.out.println("Error");
}
Try if statement
public void canDrive(int age){
if(age <= 19){
System.out.println("You are too young to drive.")
}
else{
System.out.println("You can drive!");}
}
My intention is to call two cases inside another case in the same switch statement,
switch (orderType) {
case 1:
statement 1;
break;
case 2:
statement 2;
break;
case 3:
**call case 1;**
**Call case 2;**
break;
default:
break;`
}
Can we do that in Java?
No, you can't jump to the code snippet in another switch case. You can however extract the code into an own method that can be called from another case:
switch (orderType) {
case 1:
someMethod1();
break;
case 2:
someMethod2();
break;
case 3:
someMethod1();
someMethod2();
break;
default:
break;
}
void someMethod1() { ... }
void someMethod2() { ... }
Although you cannot influence the switch cases directly, you can call switch's parent method from one case and pass different arguments. For example,
void foo(int param1, String param2, ...) {
switch (param1) {
case 0:
foo(1, "some string");
break;
case 1:
//do something
break;
default:
break;
}
}
You can't just call a another case block like this. What you could do, though, is wrap each of these blocks in a method, and reuse them:
private void doSomething() {
// implementation
}
private void doSomethingElse() {
// implementation
}
private void runSwitch(int order) {
switch (orderType) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
case 3:
doSomething();
doSomethingElse();
break;
default:
break;
}
}
Using methods is the best way to do it as mentioned in the accepted answer but for some reasons if you are unable to create method, Here is one more way to do this without using methods
boolean isBreakTheLoop = false, isCase2Required = false;
while(!isBreakTheLoop){
switch (orderType) {
case 1:
statement 1;
if(isCase2Required){
orderType = 2;
break;
}
isBreakTheLoop = true;
break;
case 2:
statement 2;
isBreakTheLoop = true;
break;
case 3:
orderType = 1;
isCase2Required = true;
break;
default:
isBreakTheLoop = true;
break;
}
}
Just a reminder: while the most suggested case is the best solution, note that if you don't close with a brake; your case:, it will continue executing in the next case.
Then, while it's still best pratice to break your cases, you could still adopt a solution similar to the following:
switch (orderType) {
case 3:
someMethod1();
case 2:
someMethod2();
break;
case 1:
someMethod1();
break;
default:
break;
}
Note that "avoiding breaks" solution can't completely cover OP necessities, because writing:
case 3:
case 1:
someMethod1();
case 2:
someMethod2();
default:
break;
or:
case 3:
case 1:
someMethod1();
break;
case 2:
someMethod2();
break;
default:
break;
would make case: 3 be the same than case: 1, making them execute both methods in first code, or a single method in the second code.
You can use this trick in some case :
switch (orderType) {
case 3:
statement 3;
case 2:
statement 2;
case 1:
statement 1;
break;
default:
break;`
}