Incredibly frustrating problem:
I have two JTextFields to accept input, the text is then checked against an array of Strings to get the index of the search result, then the indexes are subtracted to get an integer.
I have isolated the problem to the JTextFields - if I hard-code the strings to search for, I get the results I expect, but somehow, when the search text comes from the JTextField, the string is never found in the array. Full program follows:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LifeCalculator extends JFrame implements ActionListener {
//a program to calculate the number of episodes a TWD character has lived
private JPanel myPanel;
private JLabel lblInstructions, lblStart, lblDeath, lblResult;
private JTextField inStart, inDeath, outField;
//String array of episodes, Season 1 has only 6 episodes, Season 2 13 episodes, and each subsequent season has 16 episodes
private String serialEpisodes[] = {"S1E1", "S1E2", "S1E3", "S1E4", "S1E5", "S1E6",
"S2E1", "S2E2", "S2E3", "S2E3", "S2E4", "S2E5", "S2E6",
"S2E7", "S2E7", "S2E8", "S2E9", "S2E10", "S2E11", "S2E12", "S2E13",
"S3E1", "S3E2", "S3E3", "S3E3", "S3E4", "S3E5", "S3E6",
"S3E7", "S3E8", "S3E9", "S3E10", "S3E11", "S3E12", "S3E13", "S3E14", "S3E15", "S3E16",
"S4E1", "S4E2", "S4E3", "S4E3", "S4E4", "S4E5", "S4E6",
"S4E7", "S4E8", "S4E9", "S4E10", "S4E11", "S4E12", "S4E13", "S4E14", "S4E15", "S4E16",
"S5E1", "S5E2", "S5E3", "S5E3", "S5E4", "S5E5", "S5E6",
"S5E7", "S5E8", "S5E9", "S5E10", "S5E11", "S5E12", "S5E13", "S5E14", "S5E15", "S5E16",
"S6E1", "S6E2", "S6E3", "S6E3", "S6E4", "S6E5", "S6E6",
"S6E7", "S6E8", "S6E9", "S6E10", "S6E11", "S6E12", "S6E13", "S6E14", "S6E15", "S6E16"};
public LifeCalculator(){
//set up GUI
super("TWD Lifespan Calculator");
lblInstructions = new JLabel("Enter the episodes where the character was introduced and died in the proper boxes.\nEnter in the form 'SXEY' then press enter.", SwingConstants.CENTER);
lblStart = new JLabel("Episode Introduced:", SwingConstants.RIGHT);
lblDeath = new JLabel("Episode Died:", SwingConstants.RIGHT);
lblResult = new JLabel("Episode Lifespan:", SwingConstants.RIGHT);
inStart = new JTextField(10);
inDeath = new JTextField(10);
inDeath.addActionListener(this);
outField = new JTextField(10);
outField.setEditable(false);
myPanel = new JPanel();
myPanel.setLayout(new GridLayout(3,3));
myPanel.add(lblStart);
myPanel.add(inStart);
myPanel.add(lblDeath);
myPanel.add(inDeath);
myPanel.add(lblResult);
myPanel.add(outField);
Container container = getContentPane();
container.add(lblInstructions, BorderLayout.NORTH);
container.add(myPanel, BorderLayout.CENTER);
setSize(425,100);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
outField.setText("");
int startNum = 0;
int endNum = 0;
int result = 0;
String strStart = inStart.getText();
String strDeath = inDeath.getText();
//for debugging
System.out.println("====Strings from JTextFields====");
System.out.println(strStart);
System.out.println(strDeath);
System.out.println("====End Strings from JTextFields====");
for(int i = 0; i < serialEpisodes.length; i++){
if(serialEpisodes[i] == strStart){
//for debugging
System.out.println("FOUND START");
startNum = (i + 1);
}
if(serialEpisodes[i] == strDeath){
//for debugging
System.out.println("FOUND END");
endNum = (i + 1);
}
}
if(startNum != 0 && endNum != 0){
//for debugging
System.out.println("Doing calculation");
result = endNum - startNum;
}
//for debugging
System.out.println("RESULT: " + result);
outField.setText(Integer.toString(result));
}
public static void main(String args[]){
LifeCalculator app = new LifeCalculator();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I have some system prints in there to track down the exact location of my mistake. Please feel free to play along at home. Enter any two valid strings from the array and the result will always be 0. From the print lines, I get seemingly-valid results from the getText method on the JTextFields, such as "S1E1" and "S3E4" (no quotes), but for some reason, those "valid" strings are never found in my array because the two print lines in the if statement in the loop never fire.
The crazy part comes when I change lines
String strStart = inStart.getText();
String strDeath = inDeath.getText();
to a hard-coded value, like:
String strStart = "S1E1";
String strDeath = "S3E4";
Then I get expected results and it works fine. At a fundamental level, I realize that this may not be the ideal way to solve this problem, but I am not seeking architecture critiques. I have reached the point that I just want to conquer this problem. Using the results of user input in a JTextField, then searching a String array for that input does not seem like something that should be impossible to do.
Any help would be greatly appreciated.