how would i do subdirectory because i want to be able to grab all subdirectory out of a directory and still be able to get add them
I have a subdirectory called testFolder in it contains the following:
defaulfolder.1
defaultfolder
defaultfolder2.1
defaultfolder2
I only want to be able to grab the default folder without the "."
i would like to get a test output of the answers
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class checkFile {
private List<File> tests;
public List<File> getFile(String file) {
File f = new File(file);
tests = new ArrayList<File>();
if (f.isDirectory())
{
if (f.listFiles() != null)
{
if(!f.getName().contains("."))
{
for (File item : f.listFiles()) {
tests.add(item.getName());
}
}
} else {
tests.add(f.getName());
}
return tests;
}
}
public static void main(Stringg[] args)
{
String test = "C:\\User\\Name\\Desktop\\testFolder
checkFile test = new checkFile();
test.getFile(test);
}
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
public class Subdirectories {
public static final String FOLDER_PATH =
System.getProperty("user.home") // this path can be changed
+ File.separatorChar + "Desktop" // I tested on Desktop
+ File.separatorChar;
public static final String ROOT_FOLDER_NAME = "testFolder"; // root
folder name
public static void main(String[] args)
{
String FINAL_FOLDER_PATH = FOLDER_PATH + ROOT_FOLDER_NAME;
// prints all the subdirectories under the root directory
"testFolder"
System.out.println("All the directories under " + ROOT_FOLDER_NAME
+ ": ");
for(String folderName :
listFoldersInDirectory(FINAL_FOLDER_PATH))
{
System.out.println(folderName);
}
// get the folder(s) without dot in their name
System.out.println("\nAll directories without dot in their
name:");
for(String folderNameWithoutDot :
getFolderWithoutDot(listFoldersInDirectory(FINAL_FOLDER_PATH)))
{
System.out.println(folderNameWithoutDot);
}
}
public static List<String> listFoldersInDirectory(String
folderPath)
{
File folder = new File(folderPath);
FileFilter folderFilter = new FileFilter()
{
@Override
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] foldersAsFiles = folder.listFiles(folderFilter);
List<String> folderList = new
ArrayList<>(foldersAsFiles.length);
for(File folders : foldersAsFiles)
{
folderList.add(folders.getName());
}
return folderList;
}
public static List<String>
getFolderWithoutDot(List<String> folderList)
{
List<String> foldersWithoutDot = new
ArrayList<>();
for(String folderNameWithoutDot : folderList)
{
if(!folderNameWithoutDot.contains("."))
{
foldersWithoutDot.add(folderNameWithoutDot);
}
}
return foldersWithoutDot;
}
}
how would i do subdirectory because i want to be able to grab all subdirectory out...
how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...
JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...
What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...
I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...
How do I correct this error in my code?
When it got to the file type I can only go "null" from a certain
point and I'm wondering how to correct this error.
Also can I get a word document on this coding strategy for this
problem? How much of this am I doing correctly?
The original problem description:
Create a program that provides a listing of all the files in a
directory. The program should be a Java application...
I need a shoppingcartmanager.java that contains a main method for this code in java Itemtopurchase.java public class ItemToPurchase { // instance variables private String itemName; private String itemDescription; private int itemPrice; private int itemQuantity; // default constructor public ItemToPurchase() { this.itemName = "none"; this.itemDescription = "none"; this.itemPrice = 0; this.itemQuantity = 0; } public ItemToPurchase(String itemName, int itemPrice, int itemQuantity,String itemDescription) { ...
Using listAllFiles as a guide, write a method that has one File argument: if the argument is a directory, the method returns the total number of files below it. If the argument represents a file, the method just returns 1. public static int countFiles(File f) import java.io.File; public class FileLister { public static void main(String[] args) { // Choose the directory you want to list. // If running in Eclipse, "." will just be the current project directory. // Use...
(I don't need code for this question I only need explanation how to solve the question.) The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Ex02 { public static void main(String[] args) throws IOException { BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in)); ArrayList<String> myArr = new ArrayList<String>(); myArr.add("Zero"); myArr.add("One"); myArr.add("Two"); myArr.add("Three"); System.out.println(myArr.get(7)); } } Starting with this provided code,...
I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...
Can someone please help me add appropriate descriptive comments to each line of code in the project explaining why the code is in the application. import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public class BookManager { private List<Book> bookList; public BookManager() { bookList = new BookCatalog().getCatalog(); } public List<Book> getBooks(Predicate<Book> condition) { List<Book> books = new ArrayList<>(); for (Book b: bookList) { if (condition.test(b)) { books.add(b); } } return books; } } import java.util.List; public class BookManagerApp { public...