When trying to write the following code, it comes up with 3 errors. Syntax error on token 'else', insert 'assignmentoperator expression' to complete assignment and that cannot convert from string to boolean. This is for a combo box and when I choose from the combo box, I want to remove the other sites from a list. 'site' is a variable in main and c.site is a parameter in another agent. Can someone explain what to do.
if ( site ) {
if ( c.site.equals( "x" ) ) {
cavernIterator.remove();
continue;
}
}
else {
if ( c.site.equals( "y" ) ) {
cavernIterator.remove();
continue;
}
}
else {
if ( c.site.equals( "z" ) ) {
cavernIterator.remove();
continue;
}
}
else {
( c.site.equals( "a" ) ) {
cavernIterator.remove();
continue;
}
}
You can't have several else blocks for the same if. Each else block has to be attached to its own if:
if(a) {
...
} else {
if (b) {
...
}
else {
if (c) {
...
}
else {
...
}
}
}
Or, with a shorthand for all this, you can have a single-statement block inside the else, so it looks like this:
if (a) {
...
}
else if (b) {
...
}
else if (c) {
...
}
else {
...
}
This second one is nearly the same as the first, as it takes advantage of not having to wrap a statement block in braces if your statement block only has single statement, like this:
if (myCondition)
System.out.println("My condition passed");
else
System.out.println("My condition did not pass");
Else if should be written this way :
if (...) {
// Code here
} else if (...) {
// Code here
} else {
// Code here
}
Also your cases seem similar
String[] sites = {"x","y","z","a"};
boolean contains = Arrays.stream(sites).anyMatch(c.site::equals);
if (contains) {
cavernIterator.remove();
continue;
}
EDIT: Considering site is a string
String site = "x"; // Default value provided
String[] sites = {"x","y","z","a"};
boolean condition = site.equals(c.site) && Arrays.stream(sites).anyMatch(c.site::equals);
if (condition) {
cavernIterator.remove();
continue;
}
if ( site ) {
if ( c.site.equals( "x" ) ) {
}
}
equals
if(site && c.site.equals("x")){
}
And your last else is messed up.
Likely look on some Java tutorials because you write the least efficient way possible. This is your code in short:
if (site && (c.site.equals("x") || c.site.equals("y") || c.site.equals("z") || c.site.equals("a"))) {
cavernIterator.remove();
}
Related
I currently working on a project that I need to remove a class that is being used by different other classes. There are cases that I can remove the one line of code that consists of that class where it will never affect the functionality of the program, but also there are cases that the class that you want to be removed is inside an if-statement. The main problem is that once I removed the line of code consisting of that class where is it inside the if-statement, it will be an empty if-statement that will violates the sonar.
Is there another way to refactor an empty if-statement other that negating the condition of one of the statements? Because when I'm just negating the condition, the readability of the code reduced.
For Example:
if((example_A >= 0) && (condition_A))
{
removeThisClass();
}
else if((example_B >= )) && (condition_B))
{
doSomething();
}
else
{
doAnything();
}
Refactored:
if(!((example_A >= 0) && (condition_A)) && ((example_B >= )) && (condition_B)))
{
doSomething();
}
else
{
doAnything();
}
You can put this code in separate method (https://refactoring.com/catalog/extractFunction.html) and write it like this:
public void DoSomeStuff() {
if((example_A >= 0) && (condition_A))
return;
if((example_B >= )) && (condition_B)) {
doSomething();
return;
}
doAnything();
}
If I understand you right, the line removeThisClass(); should be removed, and you don't want to be left with an empty block like this:
if((example_A >= 0) && (condition_A))
{
}
else if((example_B >= )) && (condition_B))
{
doSomething();
}
else
{
doAnything();
}
In order to not do the "A" tests twice, you need to negate the condition, e.g. like this:
if ((example_A < 0) || ! (condition_A))
{
if ((example_B >= )) && (condition_B))
{
doSomething();
}
else
{
doAnything();
}
}
Your refactored code is wrong, because if the "A" condition is true, the original code would execute removeThisClass();, which means it should now do nothing, but your code will call doAnything(); when "A" is true.
You can put in a comment. Sonar should accept that and it could also help the reader.
void doSomething() {
for (int i = 0; i < 42; i++) // Non-Compliant
{
}
for (int i = 0; i < 42; i++); // Compliant
if (myVar == 4) // Compliant - contains a comment
{
// Do nothing because of X and Y
}
else // Compliant
{
doSomething();
}
try // Non-Compliant
{
}
catch (Exception e) // Compliant
{
// Ignore
}
}
I have this kind of code
if(a == 3) {
if(b == 4) {
// do somthing
} else {
// do an other thing
}
} else {
// do the same other thing
}
I wondered, when I am in the first else, how could I go to the second elsebecause it will execute the same code
Thank you
You only want the // do something part to be executed when a==3 AND b==4, so you can combine them with an && operator.
This way you can merge the two conditions into one, and have a single else clause that performs the // do an other thing part :
if(a == 3 && b == 4) {
// do something
} else {
// do an other thing
}
While I'm aware of how if/else statements and operators work (there are many posts about it), I seem to have gotten lost within this block of code in particular. I was given the final output (if else if), however I haven't been able to get to that conclusion by simply looking at it and working it out on paper. I'd really appreciate an explanation in plain terms as to what's happening in the code.
public class SimpleIsland
{
public boolean getTrue()
{
return true;
}
public boolean getFalse()
{
return false;
}
public static void myProgram()
{
if ( getFalse() || getTrue() )
{
if ( !getTrue() )
{
if ( !getFalse() )
{
System.out.println( "if if if" );
}
else
{
System.out.println( "if if else" );
}
}
else
{
if ( !getFalse() )
{
System.out.println( "if else if" );
}
else
{
System.out.println( "if else else" );
}
}
else
{
System.out.println( "else" );
}
}
}
Replace those functions and nots with the values true and false and things will become clearer. And please don't ever write code like that. It'll drive you and whoever has to maintain it crazy.
public class SimpleIsland
{
public boolean getTrue()
{
return true;
}
public boolean getFalse()
{
return false;
}
public static void myProgram()
{
if ( false || true )
{
if ( false )
{
if ( true )
{
System.out.println( "if if if" );
}
else
{
System.out.println( "if if else" );
}
}
else
{
if ( true )
{
System.out.println( "if else if" );
}
else
{
System.out.println( "if else else" );
}
}
else
{
System.out.println( "else" );
}
}
}
It will print 'if else if'. The first if(getFalse() || getTrue()) will return true since we are checking for OR condition. So after entering the first if, next nested if(!getTrue()) will return false and hence control will move to else condition. Next nested if(!getFalse()) inside else will evaluate to true, thus printing 'if else if'
I have written an ID3 and it prints the data. However, this is not very readable when the data sets become large. Is there any more human readable way this can be done?
Example of output given by the code below:
if( Outlook == "Sunny") {
if( Humidity == "High") {
PlayTennis = "No";
} else if( Humidity == "Normal") {
PlayTennis = "Yes";
}
} else if( Outlook == "Overcast") {
PlayTennis = "Yes";
} else if( Outlook == "Rain") {
if( Wind == "Weak") {
PlayTennis = "Yes";
} else if( Wind == "Strong") {
PlayTennis = "No";
}
}
I would ideally like to get a graph as follows from the above output:
Is there something I could use instead of these methods to give a tree automatically. The end goal is to analyse the tree depth etc?
Take a look at https://code.google.com/p/treelayout/
It is the layout code used by antlr and seems to work pretty well.
I have a boolean array and I want to know how to test all of them in an if statement without it taking up to much space, here is what I have so far.
private boolean[] running = new boolean[10]
if(running[] == true){
goes through code here
}
That is what I am trying to do put it won't work I don't want to have to write them all out like so.
private boolean[] running = new boolean[10]
if(running[1] == true || running[2] == true || running[3] == true || etc.){
goes through code here
}
So if there is a way to check all of them at once that would be great.
public static boolean any (boolean[] array) {
for (boolean item : array) {
if (item) {
return true;
}
}
return false;
}
if (any(running)) {
// your code
}
for(boolean bool : running) {
if(bool) {
//your code
break;
}
}
You're going to need to write a loop.
boolean success = true;
for( int i = 0; i < running.length; ++i ) {
if( running[i] == false ) {
success = false;
break;
}
}
if( success == true ) {
// Do stuff
}
You shouldn't worry about taking up too many lines of code. Just worry about writing code that's easy to understand.
EDIT:
The above runs the if statement if all of the items in the array are true. If you actually wanted to execute the code if any one item in the array is true, it would look more like this:
boolean success = false;
for( int i = 0; i < running.length; ++i ) {
if( running[i] == true ) {
success = true;
break;
}
}
if( success == true ) {
// Do stuff
}