There is a code:

public static Map<String, String> getUsers() { Map<String, String> users = new HashMap<>(); String path = "users"; try { List<String> lines = Files.readAllLines(Paths.get(path), Charset.forName("UTF-8")); try { for (String val: lines) { String[] field = val.trim().split(" "); users.put(field[0], field[1]); } } catch (ArrayIndexOutOfBoundsException | NullPointerException e) { System.out.println(e.getMessage()); } } catch (IOException e) { Logs.write("unable to open users file"); System.out.println(e.getMessage()); } return users; } 

The data is taken from the file line by line, and the values ​​are separated by commas. Then everything is recorded in the HashMap . What is the problem: containsKey() always returns false for the first HashMap element, including if it is only one. Debagger shows that this item in HashMap is.

 try { if (users.containsKey(credentials[0])) { if (users.get(credentials[0]).equals(credentials[1])) { logged = true; // db = credentials[2]; writer.println(1); } else { writer.println(0); } } else { writer.println("Suck"); } } catch (Exception e) { System.out.println(e); } 

Closed due to the fact that off-topic participants enzo , cheops, user194374 , Streletz , aleksandr barakin Jun 19, '16 at 8:49 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - enzo, cheops, community spirit, Streletz, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • а значения разделены запятыми . Attention question. Where is String[] field = val.trim().split(" "); separators commas? - Sergey
  • And I’m still interested, does HashMap have keys, is it not the result of the object’s function cache? - Maxim Drobyshev
  • @ MaksimDrobyshev is not. The hash is only suitable for relatively quick determination that objects are different. Different hash => objects are different. But the opposite is not true. Different objects can have the same hash. The hash is checked first, if it matches, then equals is called to compare objects. - Sergey
  • @Sergey was wrong, separated by spaces. I read, they say that it is necessary to redefine equals and hashCod, but I do not catch up exactly how - Shelkot
  • And interestingly, the problem affects only the first element. Is always. Regardless of its value - Shelkot

1 answer 1

Problem solved. It was all about BOM, which shoved at the beginning

  • Why not? It seems like the cause of the problem is clear. - Nick Volynkin