Создание папок из пользовательского ввода с использованием цикла while или do while - PullRequest
0 голосов
/ 26 апреля 2018

У меня проблемы с созданием нескольких папок с использованием цикла while, do-while в Java. Я попытался создать файлы, и у меня не было проблем, кроме папок, другая история.Я использовал Scanner и BufferedReader. Проблема в том, что он действует очень странно.если я наберу не менее 3 имен папок, он обычно создает только первое или пропускает второе и создает первое и третье. В конце он не примет мой «Выход» в качестве моей конечной точки, он просто создает папку с именем «Выход» и ждетдля большего вклада.Я уже делал поиск в Google, но не нашел ничего, что мне помогло. Вот код, (case AddShelf): `package library;

public class Administrator {

    public static String help_msg = "[ Main Menu ]\r\n\r\nAddBook\r\nRemoveBook\r\nBookInfo\r\nAddShelf\nRemoveShelf\r\nShelfInfo";
    private static final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

    public static void main(String[] args)throws IOException {
                        Date date = new Date();
                        System.out.println(sdf.format(date));
                System.out.println("Welcome to my book library\r\n\r\n"+"ADMINISTRATOR\r\n\r\n"+help_msg+"\r\n");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String operate = br.readLine();

    do {
        switch(operate) {
        case"AddBook":
            //chose shelf,add new book  
        case"RemoveBook":
            //chose shelf,remove book
        case"BookInfo":
            //chose shelf,chose book,show book info
        case"AddShelf":
            try {
                System.out.println("Enter the name of the shelf:");
                Scanner sc = new Scanner(System.in);
                String files = sc.nextLine();
                do {
                    File dir = new File("D:\\admir\\MyBookLibrary\\"+files);
                    dir.mkdirs();
                }while(!"Exit".equals(sc.next()));
                System.out.println("Shelf added successfully!");
            }catch(Exception e) {
                System.err.println("Unable to create new Shelf!");
            }
        case"RemoveShelf":
            //remove shelf
        case"ShelfInfo":
            //shelf info
            default:
                //if incorrect operation is entered,print message,back to choosing operations
        }
    }while(!"Exit".equals(br.readLine()));

    }
}

1 Ответ

0 голосов
/ 26 апреля 2018

Я проверял, что код делать пока немного сложно, поэтому я изменил его, пока пробовал этот код.

  import java.io.File;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Scanner;

  public class Administrator {

    public static String help_msg = "[ Main Menu ]\r\n\r\nAddBook\r\nRemoveBook\r\nBookInfo\r\nAddShelf\nRemoveShelf\r\nShelfInfo";
    private static final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

    public static void main(String[] args){
      Date date = new Date();
      System.out.println(sdf.format(date));
      System.out.println("Welcome to my book library\r\n\r\n"+"ADMINISTRATOR\r\n\r\n"+help_msg+"\r\n");

      Scanner sc = new Scanner(System.in);
      String operate;

      while(!"Exit".equals(operate = sc.nextLine())){
        switch(operate) {
          case"AddBook":
            //chose shelf,add new book
          case"RemoveBook":
            //chose shelf,remove book
          case"BookInfo":
            //chose shelf,chose book,show book info
            break;
          case"AddShelf":
            try {
              System.out.println("Enter the name of the shelf:");
              String files;
              while(!"Exit".equals(files = sc.nextLine())){
                File dir = new File("D:\\admir\\MyBookLibrary\\"+files);
                dir.mkdirs();
                System.out.println(files+"Shelf added successfully!");
                System.out.println("Enter another name of the shelf:");
              }
              return;
            }catch(Exception e) {
              System.err.println("Unable to create new Shelf!");
            }
            break;
          case"RemoveShelf":
            //remove shelf
          case"ShelfInfo":
            //shelf info
          default:
            //if incorrect operation is entered,print message,back to choosing operations
        }
      }

    }
  }
...