Please help me figure it out. A console application of the list of contacts was developed, on the task it implemented number validation if it is a number, but the code began to work strangely. The MAIN class, where the main menu occurs:

do { System.out.println("Please select action (I, N, R, L, E, Q): "); choice = sc.nextLine().trim().toUpperCase(); switch (choice) { case "I": cm.loadFromFileAndPrint(); break; case "N": String name; long number; String lengthNum; String city; do { System.out.print("Name: "); name = sc.nextLine().trim(); if (name.length() < 30) { if (cm.isNameExist(name)) break; else { System.out.println("Error: A record with such name already exists!"); } } else { System.out.println("Name is too big!"); } } while (true); do { System.out.print("Phone number: "); if (sc.hasNextLong()) { number = sc.nextLong(); lengthNum = String.valueOf(number); if (lengthNum.length() <= 12 && lengthNum.length() >= 3) { if (cm.isNumberExist(number)) { break; } else { System.out.println("Error: A record with such number already exists!"); } } else { System.out.println("Not valid number!"); } } else { System.out.println("Is not a number!"); sc.next(); } } while (true); do { System.out.print("City: "); city = sc.next(); if (city.length() < 30) break; else { System.out.println("City name should be less than 30 characters"); } } while (true); long id = cm.addContact(new Contact(name, number, city)); System.out.println("New record with ID " + id + " has been created!"); break; case "R": System.out.print("Record ID: "); long idr = Long.parseLong(sc.nextLine()); cm.deleteContact(idr); System.out.println("Record with ID " + idr + " has been removed!"); break; case "E": cm.exportToNewCSVfile("export.csv"); break; case "L": Collections.reverse(cm.getContacts()); cm.printContacts(); break; case "L:NAME": Collections.sort(cm.getContacts(), new SortedByName()); cm.printContacts(); break; case "L:NAME!": Collections.sort(cm.getContacts(), new SortedByName().reversed()); cm.printContacts(); break; case "L:PHONE": Collections.sort(cm.getContacts(), new SortedByPhone()); cm.printContacts(); break; case "L:PHONE!": Collections.sort(cm.getContacts(), new SortedByPhone().reversed()); cm.printContacts(); break; case "L:CITY": Collections.sort(cm.getContacts(), new SortedByCity()); cm.printContacts(); break; case "L:CITY!": Collections.sort(cm.getContacts(), new SortedByCity().reversed()); cm.printContacts(); break; } } while (!choice.equals("Q")); System.out.println("Bye!"); 

While adding a new contact - the option "N", the user enters the name, number, city (the city does not have to be entered). A glitch occurs after adding a city - if you do not enter the console continuously in input standby mode, if you enter a city, then the main menu message is displayed 2 times: System.out.println("Please select action (I, N, R, L, E, Q): "); Help figure out where wrong.

2 answers 2

  do { System.out.print("City: "); city = sc.next(); if (city.length() < 30) break; else { System.out.println("City name should be less than 30 characters"); } } while (true); 

Each time the do-while loop is repeated, the program first executes the body of the loop, and then evaluates the conditional expression. Means

(city is not required to enter).

in this case does not roll. Under any condition, the cycle will run at least once. According to the Scanner class api, the public Sting next () method is described as follows:

Finds and returns from this scanner. This is a complete token that has been delimiter pattern. This method can be even if a previous invocation of hasNext () returned true.

that tells us that the method can be blocked while waiting for input to be scanned. etc. Because you get

if it does not enter the console constantly in standby input

The problem output two times System.out.println ("Please select action (I, N, R, L, E, Q):");

The public Sting nextLine () method is associated with the Scanner class api:

Advances this line up. This method returns the line at the end. The position is set to the beginning of the next line. If there is no line separators for this line

which leads to an error .. see for yourself:

 public static void main(String[] args) { Scanner sc = new Scanner(new InputStreamReader(System.in)); String caseMY; do{ System.out.print("ВВЕДИТЕ next :"); caseMY = sc.next().trim().toUpperCase(); System.out.print("ВВЕДИТЕ lineg:"); caseMY = sc.nextLine().trim().toUpperCase(); }while(!caseMY.equals("Q")); } 

Well, in general, something like that), can my answer help you

    I think you have a problem in this part of the code:

      System.out.print("City: "); city = sc.next(); if (city.length() < 30) break; else { 

    In particular, review the work of your cycle.

    • After all, if your condition can not pass the test, put a break at the beginning of the else. - Sergey Richter
    • Yes, that's right, your cycle will work until you meet the If condition. I'm certainly not a master in java. But you need to figure out how to skip the loop, because you need to output a message as well. Um .. - Sergey Richter