Client: case "3": // Search Hotel // chiedo all' utente di inserire nome hotel…
- User
Client: case "3": // Search Hotel // chiedo all' utente di inserire nome hotel System.out.print("Inserisci nome hotel: "); nomeHotel = scanner.nextLine(); // chiedo all' utente di inserire città System.out.print("Inserisci citta': "); citta = scanner.nextLine(); // comunico comando al server out.println("searchHotel:" + nomeHotel + "," + citta); // ricevo la risposta dal server response = in.nextInt(); // consumo la riga vuota in.nextLine(); // controllo esito risposte server if (checkResponse(response)) { // ricevo l'hotel dal server String hotelJson = in.nextLine(); // deserializzo la stringa JSON in un oggetto Hotel Gson gson = new Gson(); Hotel foundHotel = gson.fromJson(hotelJson, Hotel.class); // stampo all'utente tutte le informazioni foundHotel.printInfoHotel(); } // Ritorna all' autenticazione authentication(); break;
Server:
public void searchHotel(String[] params, PrintWriter out) { String nomeHotel = params[0]; String citta = params[1];
codeHotel foundHotel = null; // cerco l'hotel for (Hotel h : hotels) { if (h.getName().equals(nomeHotel) && h.getCity().equals(citta)) { foundHotel = h; break; } } // se l'hotel non esiste lo comunico al client altrmimenti comunico che l'hotel è stato trovato con successo if (foundHotel == null) { out.println(ERROR_HOTEL_NOT_FOUND); return; } else { out.println(SUCCESS); } // serializzo Gson gson = new Gson(); String hotelJson = gson.toJson(foundHotel); // mando l'hotel al client out.println(hotelJson); }La seconda volta che invoco search Hotel mi da questo errore:
Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at HOTELIERCustomerClient.authentication(HOTELIERCustomerClient.java:111) at HOTELIERCustomerClient.authentication(HOTELIERCustomerClient.java:153) at HOTELIERCustomerClient.authentication(HOTELIERCustomerClient.java:125) at ClientMain.main(ClientMain.java:32)
- Assistant
The stack trace you've provided indicates that the issue happens at the line where the
nextInt()method is invoked in the method of your class. This error, , usually occurs when the object expects an (because has been called), but the input it receives is not an integer. - User
Perhaps the issue is that the client is not properly consuming the newline character(s) after receiving the initial integer response. This could result in the Scanner being in the wrong state the next time nextInt() is invoked. Your client code does consume a newline after calling nextInt(), which is correct, but ensure that no additional newlines or other unexpected input are being sent by the server and inadvertently read by the client.
Come faccio a fare questo, rispondimi in italiano
- Assistant
Per assicurarti che il client stia consumando correttamente tutte le righe di testo (inclusi i caratteri di nuova linea) inviate dal server, puoi adottare alcuni accorgimenti:
- Usa
nextLine()invece dinextInt()per leggere i codici di risposta. Potresti leggere la risposta come una stringa e poi convertirla in un intero. Questo aiuta a consumare l'intera linea, inclusi i caratteri di nuova linea.
- Usa
- User
case "3": // Search Hotel // chiedo all' utente di inserire nome hotel System.out.print("Inserisci nome hotel: "); nomeHotel = scanner.nextLine(); // chiedo all' utente di inserire città System.out.print("Inserisci citta': "); citta = scanner.nextLine(); // comunico comando al server out.println("searchHotel:" + nomeHotel + "," + citta); // ricevo la risposta dal server response = Integer.parseInt(in.nextLine()); // controllo esito risposte server if (checkResponse(response)) { // ricevo l'hotel dal server String hotelJson = in.nextLine(); // deserializzo la stringa JSON in un oggetto Hotel Gson gson = new Gson(); Hotel foundHotel = gson.fromJson(hotelJson, Hotel.class); // stampo all'utente tutte le informazioni foundHotel.printInfoHotel(); }
Ho aggiornato così ma mi da questo errore:
Exception in thread "main" java.lang.NumberFormatException: For input string: "showMyBadges Aosta" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:668) at java.base/java.lang.Integer.parseInt(Integer.java:786) at HOTELIERCustomerClient.authentication(HOTELIERCustomerClient.java:111) at HOTELIERCustomerClient.authentication(HOTELIERCustomerClient.java:151) at HOTELIERCustomerClient.authentication(HOTELIERCustomerClient.java:123) at ClientMain.main(ClientMain.java:32)
- Assistant