-2

I was wondering if it would be possible to make the code below any smaller, basically its if else statement and each time only 3 things are changing and I was think I am just repeating my self, so I though if there is any way to make it any smaller.

if (text1.getText == "1" && text2.text == "2"){
    text3.setText("3");
}

else if(text3.getText == "1" && text4.text == "2"){
    text7.setText("3");
}

else if(text5.getText == "1" && text6.text == "2"){
    text4.setText("3");
}

else if(text7.getText == "1" && text8.text == "2"){
    text5.setText("3");
}

else if(text9.getText == "1" && text10.text == "2"){
    text6.setText("3");
}

else if(text11.getText == "1" && text12.text == "2"){
    text2.setText("3");
}

else if(text13.getText == "1" && text14.text == "2"){
    text14.setText("3");
}

......

else{
    Statement here
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Seeker
  • 509
  • 4
  • 19
  • 18
    Don't use `==` for Strings. – Sotirios Delimanolis Feb 20 '14 at 21:04
  • instead should I use equal? e.g. text1.getText.equals("1") – Seeker Feb 20 '14 at 21:04
  • 7
    [Absolutely.](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sotirios Delimanolis Feb 20 '14 at 21:04
  • 1
    Should `getText` be `getText()`? – SimonT Feb 20 '14 at 21:05
  • 1
    @SimonT, yes it should be, I had to rewrite the code on stackoverflow so I forgot to add that, sorry about that. – Seeker Feb 20 '14 at 21:06
  • 1
    Use a collection of JTextFields such as an `ArrayList`, a for loop, and as noted above a correct test of equality. I have to wonder if a JTable would not be better here. – Hovercraft Full Of Eels Feb 20 '14 at 21:08
  • Using the modulus operator would make the code smaller, but it would also make it a lot more unreadable, so I wouldn't recommend it. – kabb Feb 20 '14 at 21:08
  • @HovercraftFullOfEels, could you please give me an example. – Seeker Feb 20 '14 at 21:10
  • In every if block you are checking for different variables.... in such situations... you can't reduce the code any further... Sometimes its its better to leave the code as its for better readability than to put multiple check in one single confusing block make it more error prone and difficult to debug... – Jay Feb 20 '14 at 21:10
  • Perhaps you can create a function that takes three arguments, checks that the first two texts' values equal "1" and "2", and then sets the third text to "3". Then, make a collection of lists of text fields and iterate through it, passing the first, second, and third element of each list to the function. – BVB Feb 20 '14 at 21:11
  • I tried making a method for only the if and else if statement and then calling the method for how many times I need it but then the else statement don;t work. – Seeker Feb 20 '14 at 21:11
  • 1
    Also, why should I post an answer when you have ignored the effort I put into answering [this previous question](http://stackoverflow.com/questions/21836049/how-to-get-and-print-the-name-of-a-variable-which-holds-a-jbutton) of yours? Normally this doesn't bother, me, but I put a lot of time into that answer and you completely blew it off. – Hovercraft Full Of Eels Feb 20 '14 at 21:12
  • I would suggest putting the pieces of this puzzle into a Karnaugh Map and letting it reduce itself. The result is guaranteed to be the MOST simplified boolean logic possible. – Rainbolt Feb 20 '14 at 21:15
  • You can also use equalsIgnoreCase if there is no Case Sensative issues Example: `text1.getText().equalsIgnoreCase("1") && text2.getText().equalsIgnoreCase("2")` – Suzon Feb 20 '14 at 21:15
  • @HovercraftFullOfEels, sorry about that but the post was marked at off-topic and it wouldn't allow me to edit or comment on anything, the best I could do was rate the question – Seeker Feb 20 '14 at 21:15
  • 2
    That question wasn't even closed, but if it were, you are allowed to 1) comment on any answer, even if the question is closed, and 2) edit your question to improve it as we suggested you do. Closed questions often get re-opened if improved. I ask that you do both of these things out of respect for the efforts others have put into trying to answer your question. – Hovercraft Full Of Eels Feb 20 '14 at 21:17
  • @HovercraftFullOfEels, I am sorry for what have happened, is there anything I can do now to make you happier and not have hard feeling for me? – Seeker Feb 20 '14 at 21:32
  • Please resolve the prior question. Accept the best answer, comment on the answers given. – Hovercraft Full Of Eels Feb 20 '14 at 21:49
  • @Suzon `equalsIgnoreCase` can be ignored. – SimonT Feb 20 '14 at 23:08

3 Answers3

1

The pattern looks like for every pair of textN and textN+1, you set some other textM to 3 if the two aforementioned are labelled 1 and 2 respectively. I don't see the pattern connecting pairs to their target texts, so I suggest this:

  1. Assuming these text variables are of the type Text, store them using one of the following:
    • Text[] texts = new Text[N];
    • ArrayList<Text> texts = new ArrayList<Text>();
  2. Put all of your Text instances in the array or ArrayList
  3. Set up a map or method that takes the odd number of some pair of consecutive Texts and returns the target Text to modify (look up HashMap)
  4. Use a for loop to iterate through every other Text instance

Here's an example, if you're using an array and some method findTarget:

for (int i = 0; i+1 < texts.length; i+=2) {
    if texts[i].getText().equals("1") && texts[i+1].getText().equals("2") {
        texts[findTarget(i)].setText("3");
        break;
    }
}
SimonT
  • 2,219
  • 1
  • 18
  • 32
1
boolean didIt = false;
ArrayList<JTextField[]> listOfStuff = new ArrayList<JTextField[]>();
listOfStuff.add({ text1, text2, text3 });
listOfStuff.add({ text3, text4, text7 });
....
listOfStuff.add({ text13, text44, text14 });

for (JTextField[] fields : listOfStuff) {
    didIt = didIt || setToThree(fields[2], fields[0], fields[1]);
}

if (!didIt) {
    Statement here
}

Function:

boolean setToThree(JTextField target, JTextField first, JTextField second) {
    if ("1".equals(first.getText()) && "2".equals(second.getText()) {
        target.setText("3");
        return true;
    }
    return false;
}
BVB
  • 5,380
  • 8
  • 41
  • 62
  • Hi, BVB, could you please briefly explain the code. – Seeker Feb 20 '14 at 21:26
  • 1
    I have simply created a list of lists of text fields. For each list of text fields, a function is called. If a text field is modified within the function, true is returned. This return value is read into the didIt boolean, which controls whether the "else" block from the original code should be executed. – BVB Feb 20 '14 at 23:09
0

You can create a function to do that.

function CheckValues (a, b, c){
    if (a.getText().equals("1") && (b.getText().equals("2")){
        c.setText("3");
    }

}

And then run it with:

CheckValues(text1,text2,text3);
CheckValues(text3,text4,text7);
CheckValues(text5,text6,text11);
Renato Galvones
  • 533
  • 1
  • 3
  • 14