-2

I need some help with this problem - Java's startsWith() method doesn't seem to be working.

Does someone know why is this happening?

The method:

public boolean check(File f) {
    boolean ans = (f.getName().startsWith(this.pre));
    System.out.println(f.getName()+"     "+this.pre);
    System.out.println(ans);
}

The output I get is:

file5.b     file 
false
Same_a.txt     file 
false
same_name_a.txt     file 
false
same_name_c.txt     file 
false

It looks like it's always returning false.

EDIT: You were right and I misunderstood some things... thanks and sorry for the inconvinnience.

Re'em
  • 1,869
  • 1
  • 22
  • 28
  • 3
    What's the value of `pre`? – Anubian Noob May 29 '14 at 01:19
  • try adding single quotes around the string output. i bet there is extra whitespace around the values. – jtahlborn May 29 '14 at 01:22
  • When I went and edited the question text, I noticed that in your output lines, there was always a space after the word "file" when you print `this.pre`. Are you sure that its value is `"file"` and not `"file "`? – Dennis Meng May 29 '14 at 01:31
  • 2
    Also, get into the habit of approaching these problems with the mindset that more than 99.99% of the time, the error is going to be in your code, not in the Java standard library. Wording a question as if it is a bug in the standard library is almost a surefire way of getting downvoted. – Dennis Meng May 29 '14 at 01:35
  • Lacks minimal info to diagnose the problem. – Anubian Noob May 29 '14 at 01:53

2 Answers2

0

Try this:

public boolean check(File f) {
   return f.getName().replaceAll("\\s+","").startsWith(this.pre.replaceAll("\\s+",""));
}

This will remove all whitespaces from f.getName() and this.pre and then compare them (without the whitespace).

There may also be an issue with case-sensitivity, so you make want to lowercase all your chars with .toLowerCase().

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • Done. Still not helping :\ – Re'em May 29 '14 at 01:33
  • then you've an error somewhere else. I've just tried it a few seconds ago. Please show some more context, maybe you've some invisible chars in your filename. – Emanuel May 29 '14 at 01:34
0

Java's startsWith() method doesn't seem to be working.

I must break it to you that the startWith() method does work. It is used by (I guess) millions of Java programmers / programs. The chances of a real bug in that method are vanishingly small. You should assume the problem is in your code ... unless you have watertight evidence that it isn't.

If a library method like that appears to be not working, that is either because you are not using it correctly, or because it actually >>is<< working and there is a problem with your inputs that you did not consider.

I cannot see anything wrong with the way you are using it, so the likely explanation is that the inputs are at fault.

  • The most likely explanation is that pre has leading or trailing whitespace characters that you haven't noticed. Putting quotes around the strings in the println expression is a simple way to spot this.

  • The other possibility is "homoglyphs". Somehow, either the name string or the pre string contains characters that look the same when you display them but are actually different Unicode codepoints. For example "Latin C" and "Cyrillic C" will most likely look identical on your screen ... but the codepoints are different and will compare as different.

    If you have this problem, it may be simplest to spot using a debugger; i.e. set a breakpoint and single-step through the startsWith call to see what characters are being

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216