while (true){ String key = reader.readLine(); Movie movie = MovieFactory.getMovie(key); if (!(key.equals("cartoon") || key.equals("thriller") || key.equals("soapOpera"))) { reader.close(); break; } else { //Movie movie = MovieFactory.getMovie(key); System.out.println(movie.getClass().getSimpleName()); } } In the beginning i call MovieFactory.getMovie method in the else part , it fail to pass, so i move it before if and passed. Can anyone explain why i am wrong in the beginning? Thank you!