1

Followup to this Q&A.

The aim is to match a String, which is returned by the following method:

public String getAssignment() {
    return (String) hashmap.get("assignment");
}

Attempt

As the following:

String s = "12345";
System.out.println(s.matches("12345"));

results in a match, the idea was that:

System.out.println(assignment.getAssignment());
System.out.println(assignment.getAssignment().length());
System.out.println(assignment.getAssignment().matches("11111"));
System.out.println(assignment.getAssignment().equals("11111"));
System.out.println(assignment.getAssignment().trim().length());

should return true as well.

Result

System.out 11111
System.out 15
System.out false
System.out false
System.out 13
Community
  • 1
  • 1
030
  • 10,842
  • 12
  • 78
  • 123
  • Working for me. Could you do a reproductible example? – user2336315 Oct 29 '14 at 21:53
  • @aruisdante Yes, `11111` is returned according System.out – 030 Oct 29 '14 at 21:55
  • 1
    Most probably the return value of `assignment.getAssignment()` contains some `whitespace` that is not visible in `sout` but makes the string does not match `11111` without these whitespaces. Use a debugger and inspect the return value of `assignment.getAssignment()` or check String's `length`. – Fabian Barney Oct 29 '14 at 21:55
  • @user2336315 Should I include the HashMap? – 030 Oct 29 '14 at 21:55
  • @FabianBarney I will check the String. – 030 Oct 29 '14 at 21:57
  • @FabianBarney `System.out.println(assignment.getAssignment().matches("1 1 1 1 1 "));` does not work either – 030 Oct 29 '14 at 22:00
  • @FabianBarney Good suggestion. I will check the length of the String. – 030 Oct 29 '14 at 22:01
  • @utrecht Uhrm, that's not what I meant when I talked about whitespace before. – Fabian Barney Oct 29 '14 at 22:02
  • 1
    If you just want to check that a value is *equal* to some constant, use `.equals()`. `.matches()` is for regex matching, which is unnecessary for checking against simple String literals. – Bohemian Oct 29 '14 at 22:02
  • @utrecht You may want to inspect the byte array resulting for the String. There might be non printable characters as well. – user2336315 Oct 29 '14 at 22:03
  • Your title is meaningless. A string can't match System.out. – user207421 Oct 29 '14 at 22:22

1 Answers1

2

Most likely, you have "invisible" characters (whitespace) after the value - call trim() on the value before testing it.

Also, your check doesn't need regex, so just use .equals() instead of .matches().

Try this:

assignment.getAssignment().trim().equals("11111")

Edit

You seem to have embedded whitespace - no problem. To remove all whitespace from anywhere in the value, use replaceAll() (for regex-based replacement):

assignment.getAssignment().replaceAll("\\s", "").equals("11111")
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thank you for posting an answer. The length of the string has been reduced from 15 to 13 characters using trim. The expectation was that `trim` would result in 5 characters. I will continue to debug the string. – 030 Oct 29 '14 at 22:14
  • 1
    @utrecht If you want some tips on what could have happened to it, you can use the following monstrosity to print it out in unambiguous base64 that anyone can look at without losing information: `System.out.println(javax.xml.bind.DatatypeConverter.printBase64Binary(assignment.getAssignment().getBytes(java.nio.charset.Charset.forName("UTF-8"))));` – that other guy Oct 29 '14 at 22:21
  • 1
    Or something simpler that will give you the character codes: `for ( char ch : assignment.getAssignment().toCharArray() ) System.out.printf("%x%n",(int)ch);` – RealSkeptic Oct 29 '14 at 22:32
  • @RealSkeptic the result is: `9` – 030 Oct 29 '14 at 22:40
  • @RealSkeptic I am lost. When I run the code the output is: `11111`, `31`, `9`, `9`, `31`, `9`, `9`, `31`, `9`, `9`, `31`, `9`, `9`, `31`, `9`, `9` – 030 Oct 29 '14 at 22:52
  • You seem to have two *tab* characters following each of your *1*'s. I have no idea why the output is not showing them widely tabbed, but that's certainly "1\t\t1\t\t1\t\t1\t\t1\t\t" and not "11111". – RealSkeptic Oct 29 '14 at 23:01
  • @Bohemian Thank you. I will try this solution this evening and I will let you know the outcome. – 030 Oct 30 '14 at 09:10