简体   繁体   中英

Creating folders from user input using while or do while loop

I'm having problem creating multiple folders using while,do-while loop in Java.I tryed creating files and i had no problems but folders,different story. I was using Scanner and BufferedReader.The problem is that,it acts realy wierd. if i type at least 3 folder names,it would usually create only first,or skip second and create first and third.At the end it wont accept my "Exit" as my finish point it would just create folder named "Exit" and wait for more input. I already did search in google but didnt found anything that helped me.Here is the code,(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()));

    }
}

I checked code do while is bit difficult, So I changed it to while try this code.

  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
        }
      }

    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM