Reputation: 13
All I have to know is how to write this code shorter (if possible). Could someone take a look to it?
I couldn't find how I could write it in a shorter way, but maybe someone knows a way to do it. Maybe I can put all this code in only 1 If-statement or something?
String arrayWoord[]=new String[10];
arrayWoord[0] = letterVeld1.getText();
arrayWoord[1] = letterVeld2.getText();
arrayWoord[2] = letterVeld3.getText();
arrayWoord[3] = letterVeld4.getText();
arrayWoord[4] = letterVeld5.getText();
arrayWoord[5] = letterVeld6.getText();
arrayWoord[6] = letterVeld7.getText();
arrayWoord[7] = letterVeld8.getText();
arrayWoord[8] = letterVeld9.getText();
arrayWoord[9] = letterVeld10.getText();
if (arrayWoord[0].equals("")) {
toonKnop1.setVisible(false);
letterlijnLabel1.setVisible(false);
} // end of if
if (arrayWoord[1].equals("")) {
toonKnop2.setVisible(false);
letterlijnLabel2.setVisible(false);
} // end of if
if (arrayWoord[2].equals("")) {
toonKnop3.setVisible(false);
letterlijnLabel3.setVisible(false);
} // end of if
if (arrayWoord[3].equals("")) {
toonKnop4.setVisible(false);
letterlijnLabel4.setVisible(false);
} // end of if
if (arrayWoord[4].equals("")) {
toonKnop5.setVisible(false);
letterlijnLabel5.setVisible(false);
} // end of if
if (arrayWoord[5].equals("")) {
toonKnop6.setVisible(false);
letterlijnLabel6.setVisible(false);
} // end of if
if (arrayWoord[6].equals("")) {
toonKnop7.setVisible(false);
letterlijnLabel7.setVisible(false);
} // end of if
if (arrayWoord[7].equals("")) {
toonKnop8.setVisible(false);
letterlijnLabel8.setVisible(false);
} // end of if
if (arrayWoord[8].equals("")) {
toonKnop9.setVisible(false);
letterlijnLabel9.setVisible(false);
} // end of if
if (arrayWoord[9].equals("")) {
toonKnop10.setVisible(false);
letterlijnLabel10.setVisible(false);
} // end of if
Upvotes: 0
Views: 79
Reputation: 201447
Assuming the various Label
(s) are instances of JLabel
and that the various Knop
(s) are JComponent
(s) - you could populate three arrays and use a single for
loop. Something like,
String arrayWoord[] = { letterVeld1.getText(), letterVeld2.getText(),
letterVeld3.getText(), letterVeld4.getText(), letterVeld5.getText(),
letterVeld6.getText(), letterVeld7.getText(),
letterVeld8.getText(), letterVeld9.getText(), letterVeld10.getText() };
JLabel[] letterLabels = { letterlijnLabel1, letterlijnLabel2, letterlijnLabel3,
letterlijnLabel4, letterlijnLabel5, letterlijnLabel6, letterlijnLabel7,
letterlijnLabel8, letterlijnLabel9, letterlijnLabel10 };
JComponent[] knops = { toonKnop1, toonKnop2, toonKnop3, toonKnop4, toonKnop5,
toonKnop6, toonKnop7, toonKnop8, toonKnop9, toonKnop10 };
for (int i = 0; i < arrayWoord.length; i++) {
if (arrayWoord[i].isEmpty()) {
knops[i].setVisible(false);
letterLabels[i].setVisible(false);
}
}
And, if you're using Java 8+, the for
loop could be rewritten with an IntStream
, a filter
and a forEach
like
IntStream.range(0, arrayWoord.length).filter(i -> arrayWoord[i].isEmpty())
.forEach(i -> {
knops[i].setVisible(false);
letterLabels[i].setVisible(false);
});
Note that String.isEmpty()
returns true iff the String
has a length of 0
.
Upvotes: 4
Reputation: 2096
String[] arrayWoord = {
letterVeld1.getText(),
letterVeld2.getText(),
...
letterVeld10.getText()
};
TypeOfLetterlijnLabels[] letterlijnLabels = {
letterlijnLabel1,
letterlijnLabel2,
...
letterlijnLabel10
};
TypeOfToonKnop[] toonKnops = {
toonKnop1,
toonKnop2,
...
toonKnop10
}
for(int i = 0; i < arrayWoord.length && i < letterlijnLabels.length; i++) {
if(arrayWoord[i].equals("")) {
toonKnops[i].setVisible(false);
letterlijnLabels[i].setVisible(false);
}
}
Upvotes: 0
Reputation: 36
import org.apache.commons.lang3.StringUtils;
for(String string : arrayWoord) {
if(StringUtils.isNotBlank(string)) {
// get toonKnop for arrayWoord
// get letterlijnLable for arrayWoord
}
}
Upvotes: 0
Reputation: 4282
You can try using for
loop to make it shorter:
for (String word : arrayWoord) {
if (word.equals("")) {
// put your code here..
}
}
Upvotes: 2
Reputation: 270
Iterate through arrayWooord with a for loop and put conditionals within that for loop to complete the task you're trying to do.
Upvotes: 0