Question

I posted a question a few minutes ago and it was requested that I post an "SSCCE" to demonstrate my issue. I have heavily condensed my program so if a few constructs make no sense or seem even more ineffcient than a novice's work ought to be that's simply a product of that operation.

Now, my program essentially serves to copy an array of Russian verbs conjugations to a centralized array, oneRay. In the setup here, a prompt is shown above a blank line, and in that blank line one is to type the appropriate conjugation. Pressing the "Submit" button is supposed to check the answers against those of the array, but by my own fault or lack of understanding I see the message "FAIL" even when directly copying what I know to be the correct answer. With this line I get the input:

build1 = new StringBuilder(ssfield1.getText());

and with this I check it against the element:

if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
    else{ssfield1.setText("FAIL");}

I feel that this may be a simple issue of checking the wrong element against the wrong input textfield, however it mayn't be, so here is all the code of my condensed "SSCCE":

Not to long I hope, and certainly self-contained (and quite simple too): Let me know if you need anything else, or if it's too late for these questions!

package wbh;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class SwingImplementation_RUS extends JFrame{
String [] [] oneRay;Random random = new Random();
JPanel panel; JTextField field,sfield1,sfield2,ssfield1,ssfield2; JButton buton;
int pick, hold;StringBuilder build1,build2;
public SwingImplementation_RUS(final int subj){
    super("To be updated"); 
    final String RUS_1[][]={
        {"знать","знаю","знаешь","знает","знает","знает","знаем","знаете","знают",}};
    new Thread(new Runnable() {
            public void run() {
                buton = new JButton("Submit");
                setLayout(new GridLayout(3,1,3,3));
                panel = new JPanel();panel.setLayout(new GridLayout(5,2,3,3));
                field = new JTextField();
                sfield1 = new JTextField("Я");sfield2 = new JTextField("Ты");
                ssfield1 = new JTextField("");ssfield2 = new JTextField("");

                buton.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent m) {
                        System.out.println("CHECKED");
                        build1 = new StringBuilder(ssfield1.getText());
                        build2 = new StringBuilder(ssfield2.getText());
                        called2();}}); 

                Font f = new Font("Arial", Font.BOLD, 75);
                field.setFont(f);

                add(field);
                add(panel);
                panel.add(sfield1);panel.add(ssfield1);
                panel.add(sfield2);panel.add(ssfield2);
                add(buton);
                setLocation(500,0);setSize(865, 700);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);
                }}).start();

                oneRay = new String[RUS_1.length][9];
               for(int i = 0; (RUS_1.length) > i; i++){
                   oneRay[i][0] = RUS_1[i][0];oneRay[i][1] = RUS_1[i][1];
                   oneRay[i][2] = RUS_1[i][2];} 

        try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
        hold = oneRay.length;pick = random.nextInt(hold);
        field.setText("                 "+oneRay[pick][0]); 
}
private void called2(){
    if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
    else{ssfield1.setText("FAIL");}
    if(build2.equals(oneRay[pick][2].toLowerCase().replaceAll("\\s","")))ssfield2.setText("CORRECT");
    else{ssfield2.setText("FAIL");}

}
public static void main (String [] args){
    SwingUtilities.invokeLater(new Runnable(){
        public void run() {
        new SwingImplementation_RUS(1);
        } 
    });
}

}

Was it helpful?

Solution

I think the problem is that you are using StringBuilder#equals and expected it to work like String#equals.

The implementation of equals for StringBuilder looks like...

public boolean equals(Object obj) {
    return (this == obj);
}

Which is just comparing the object references, not there contents.

Instead, try using something like...

if (build1.toString().equals(oneRay[pick][1].toLowerCase().replaceAll("\\s", ""))) ...

As a recommendation, you should also use {} around your if-else conditions, for example

if (build1.toString().equals(oneRay[pick][1].toLowerCase().replaceAll("\\s", ""))) {
    ssfield1.setText("CORRECT");
} else {
    ssfield1.setText("FAIL");
}
if (build2.toString().equals(oneRay[pick][2].toLowerCase().replaceAll("\\s", ""))) {
    ssfield2.setText("CORRECT");
} else {
    ssfield2.setText("FAIL");
}

Which will make it easier to read (generally) and ensure that you are not accidently executing functionality you didn't expect for a condition...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top