0

Please help me with array in java, when entering name it doesn't accept space

import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Welcome!"); Scanner scanner = new Scanner(System.in); String[] hotel = new String[10]; while (true) { System.out.print("Enter room number: "); int roomNumber = scanner.nextInt(); System.out.print("Enter customer name: "); String customerName = scanner.next(); hotel[roomNumber] = customerName; System.out.print("Do you want to check in another customer (yes/no)?"); String answer = scanner.next(); if (answer.equalsIgnoreCase("no")) { break; } } System.out.println("All customer records:"); for (int i = 0; i < hotel.length; i++) { if (hotel[i] != null) { System.out.println("Room " + i + ": " + hotel[i]); } } } }

15th Dec 2022, 1:28 AM
Tori Kim
Tori Kim - avatar
1 Odpowiedź
+ 2
Use scanner.nextLine() instead of next()
15th Dec 2022, 4:46 AM
Tibor Santa
Tibor Santa - avatar