Good evening guys! I need to rewrite this program so that it asks the user for t
ID: 3739989 • Letter: G
Question
Good evening guys! I need to rewrite this program so that it asks the user for the location where the new directories are to be created, and then asks the user to enter, one at a time, the relative path names of the directories it should create.
package createdirectoriesdemo;
import java.io.*;
import java.util.Scanner;
public class CreateDirectoriesDemo {
public static void main(String[] args) {
// Establish the location of the parent for the new set of directories.
// This could be changed to user input.
String location = "c:/";
// create a String array of the directories to be created
String[] folderPaths = {
"/Spring Semester",
"/Spring Semester/ENGL 101",
"/Spring Semester/CSCI 111",
"/Spring Semester/MATH 163",
"/Spring Semester//PHYS 111",
"/Spring Semester/CSCI 111/programs",
"/Spring Semester/CSCI 111/docs"
};
// create a File class array for directories to be created
File[] newFolders = new File[folderPaths.length];
// create new directories based on the file names in the array
for (int i = 0 ; i < newFolders.length; i++)
{
// create a File object for this new directory
// based on the parent location and each new path name
newFolders[i] = new File( location + folderPaths[i] ) ;
// make the new directory
newFolders[i].mkdir();
} // end for
} // end main()
} // end class CreateDirectoriesDemo
Explanation / Answer
package createdirectoriesdemo; import java.io.*; import java.util.Scanner; public class CreateDirectoriesDemo { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Establish the location of the parent for the new set of directories. // This could be changed to user input. //String location = "c:/"; System.out.print("Enter a location: "); String location = in.nextLine(); // create a String array of the directories to be created // String[] folderPaths = { // "/Spring Semester", // "/Spring Semester/ENGL 101", // "/Spring Semester/CSCI 111", // "/Spring Semester/MATH 163", // "/Spring Semester//PHYS 111", // "/Spring Semester/CSCI 111/programs", // "/Spring Semester/CSCI 111/docs" // }; System.out.println("How many directories: "); int size = Integer.parseInt(in.nextLine()); String folderPath; // create a File class array for directories to be created File[] newFolders = new File[size]; // create new directories based on the file names in the array for (int i = 0 ; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.