I am trying 2 write a program to solve a soduko.
I have lots of text fields and I need to compare them like
if (string1.equals(textfield){ do whatever}

but it only works for "0" e.g null??/
I have tried parsing it to an int but get the same result.
any ideas

Dani AI

Generated

As pointed out, calling the text-field API was the right move and confirmed it worked. A few common causes for string-comparison surprises in Swing programs: using == instead of .equals, invisible leading/trailing characters, calling .equals on a null reference (NullPointerException), or repeatedly reading UI text during a solver instead of working on a numeric model.

Safe, minimal patterns that avoid those traps:

String s = myTextField.getText();
s = (s == null) ? "" : s.trim();      // remove accidental spaces
if ("1".equals(s)) {                  // literal.equals(var) guards against NPE
    // handle digit 1
}

When the intent is numeric comparison, parse with a try/catch after trimming:

try {
    int v = Integer.parseInt(s);
    if (v == 1) { /* handle */ }
} catch (NumberFormatException ex) {
    // empty or invalid input -> treat as blank cell
}

For a Sudoku solver, convert the GUI into an int[][] once (use 0 for blank cells) and run the solver on that model; update the JTextFields only after the algorithm finishes. Also restrict input at entry (DocumentFilter, InputVerifier, or JFormattedTextField) so fields accept only single digits 1–9 — this avoids whitespace and format issues at the source. These steps prevent subtle equality failures and make the solving logic simpler and more robust.

Recommended Answers

All 2 Replies

Try using this:
String value = myTextField.getText();

I thought I tried that but I got it to work cheers mate, he's what I was using for anyone with the same problem

String mydata =  numberGrid[j].getText();


if (mydata.equals("1")){
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.