When transferring keys of type Iterator , an output problem occurs in the valueChecker() method. I wrote in the comments to the code what is wrong with my result. What am I doing wrong ?

 public Map<Class, Object> JSONFormatStringSeparator(JSONObject jsonObject) { Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()){ String key = keys.next(); System.out.println(key); // выводит все ключи } String value = valueChecker(keys,jsonObject); // вызываю метод и передаю ключи и сам Json object System.out.println("After value checker!"); // выводит if(value!=null) { switch (value) { case "create-company": String [] array = {"id","compname","password","email"}; if(keyChecker(keys,array) == array.length){ System.out.println("All found"); Company company = new Company(jsonObject.getLong("id"),jsonObject.getString("compname"), jsonObject.getString("password"),jsonObject.getString("email")); return objectMapper(Company.class,company); } } } return null; } private String valueChecker(Iterator<String> keys,JSONObject jsonObject){ System.out.println("in the value checker!"); // выводит while(keys.hasNext()){ // не заходит и метод возврашает null! String key = keys.next(); System.out.println("in value checker loop!"); if(key.equals("formId")){ return (String)jsonObject.get(key); } } return null; } 

    2 answers 2

    You get an iterator and scroll through your collection to the end:

     Iterator<String> keys = jsonObject.keys(); while (keys.hasNext()){ String key = keys.next(); System.out.println(key); } 

    Next, you pass an iterator to the valueChecker(...) method, and at that moment the iterator is in the last position, then keys.hasNext() returns false , and your method returns null .

    To solve this problem, after displaying all the keys (and before calling the valueChecker(...) method), request the iterator again:

     keys = jsonObject.keys(); 

    In general, since you are passing the jsonObject itself to the method, there is no sense in transmitting the iterator. Just get in this method an iterator from the passed jsonObject object.

      The problem is that you work with the same iterator and in the very first cycle you reach its end, so subsequent calls to the keys.hasNext () method do not return anything.
      Since you cannot reset the iterator to the beginning in java, you need to call jsonObject.keys () every time:

       String value = valueChecker(jsonObject.keys(), jsonObject); 

      Only in this case there is absolutely no reason to transfer both the keys and the object - it is easier to transfer the json object, and get the keys inside the method:

       private String valueChecker(JSONObject jsonObject) { Iterator<String> keys = jsonObject.keys(); ... } 

      But your entire valueChecker method is somehow incorrect - neither the iterator nor the loop is needed there. It is much easier to write it this way (and at the same time give the method a meaningful name):

       private String getFormAction(JSONObject jsonObject) { if (jsonObject.has("formId")) { return jsonObject.getString("formId"); } return null; }