Here is the code that adds the machine manufacturer.

String empty = ""; String manufacturer; System.out.print("Please enter car's manufacturer: "); do { manufacturer = System.console().readLine(); if (Objects.equals(manufacturer, empty)) { System.out.print("You didn't enter car's manufacturer! Please enter again: "); } } while (Objects.equals(manufacturer, empty)); carStorage.newCar(manufacturer); System.out.print("\nYour manufacturer is added!"); 

But there is one such bug, if I put a space instead of an empty value, it will count it and the code will perform its action and put it into the carStorage. So I decided to create an array with my 'spaces'

String empty[] ={""," "," "," "," "," "," " и т.д.}

it does not compare with these values. How else is this business to implement it? So that the spaces allowed in the beginning are not readable, and even if the user entered 100 spaces, said that you did not enter a name?

  • four
    I do not quite understand what your code is doing, and who creates CLEARS! If you get rid of the spaces use String.trim () - Denis Kotlyarov
  • @DenisKotlyarov Nobody creates spaces, just when I run the program, it requires you to enter the name of the manufacturer of the machine, if you leave the value blank and press Enter, it will return back to say that you did not enter anything. Nooo if I take a click on the spacebar and click on Enter, it will accept it as text and say that your car is added - E1mir
  • 2
    @KryTer_NexT see what the user entered. If after the trim operation, the string is empty - do not enter anything into the array and ask the user to translate the data ..... that is, compare not with the manufacturer , but with the manufacturer.trim() - Alexey Shimansky
  • @ AlekseyShimansky Ooooo daa) This is what I need)) Thank you) It worked) - E1mir
  • one
    Danu :)) _____________________ - Denis Kotlyarov

1 answer 1

  do { manufacturer = System.console().readLine(); if (Objects.equals(manufacturer.trim(), empty)) { System.out.print("You didn't enter car's manufacturer! Please enter again: "); } } while (Objects.equals(manufacturer.trim(), empty)); 
  • you can even Objects.equals (manufacturer.trim (). isEmpty ()); - Senior Pomidor