Question

/**    *    * @param extension    * @return number of files with th...

/**
   *
   * @param extension
   * @return number of files with the given extension
   * (case insensitive)
   * NOTE: don't compare Strings using ==
   * Google "String case insensitive comparison java"
   */
   public int getCountByExtension(String extension) {
  
       return 0; //to be completed
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import java.io.File;
import java.io.FilenameFilter;
import java.util.Scanner;


public class CountFileExtension
{

    public static void main(String[] args)
    {
        Scanner scnr=new Scanner(System.in);
    System.out.print("Enter extension: ");
           String ext=scnr.nextLine();
     System.out.print("Number of files: "+getCountByExtension(ext));
    }
    public static int getCountByExtension(String extension)
    {
        int count=0;
        File f = new File("C:\\Users\\Asha Ali\\Documents\\NetBeansProjects");
       
      
       for (File file : f.listFiles()) {
                       
                            if(file.toString().toLowerCase().endsWith(extension))
                            {
                                count++;
                        }
                }

        return count;
    }
}

Output

Enter extension:
.txt
Number of files: 22

Add a comment
Know the answer?
Add Answer to:
/**    *    * @param extension    * @return number of files with th...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Public class File {    public String base; //for example, "log" in "log.txt"  &nbs...

    public class File {    public String base; //for example, "log" in "log.txt"    public String extension; //for example, "txt" in "log.txt"    public int size; //in bytes    public int permissions; //explanation in toString public String getExtension() {        return extension;    } public void setExtension(String e) {           if(e == null || e.length() == 0)            extension = "txt";        else            extension = e;    } public class FileCollection {    private File[] files;    /**    * DO NOT MODIFY    * Loads collection from input file    * @param input: name...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary...

    Need Help Using Java programming only import java.util.ArrayList; //Import anything you need here public class Dictionary {     // Declare any variables you feel necessary here     public Dictionary(/*DO NOT PUT ANY PARAMETERS HERE!*/) {         // Initialise those variables     }     /***      * @return number of terms currently in the dictionary hint: starts at 0 when      *         there are no terms      */     public int numberOfTerms() {         return 0;     }     /***     ...

  • Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement...

    Java, can you help me out thanks. CSCI 2120 Introduction For this assignment you will implement two recursive methods and write JUnit tests for each one. You may write all three methods in the same class file. You are required to write Javadoc-style documentation for all of your methods, including the test methods. Procedure 1) Write method!! a recursive method to compare two Strings using alphabetical order as the natural order (case insensitive, DO NOT use the String class built-in...

  • Given a String str, and a char c return the number of times that c appears...

    Given a String str, and a char c return the number of times that c appears inside str. countChar("Abcdefg", 'a') returns 1 countChar("xxXx", 'x') returns 4 countChar("", 'q'') returns 0 ----------------- Please use java public class Count { public int countProblem(String str, char c) { int res = 0; //Your work is here return result; } }

  • Need help coding this List. Lists are a lot like arrays, but you’ll be using get,...

    Need help coding this List. Lists are a lot like arrays, but you’ll be using get, set, add, size and the like instead of the array index operators [].​ package list.exercises; import java.util.List; public class ListExercises {    /**    * Counts the number of characters in total across all strings in the supplied list;    * in other words, the sum of the lengths of the all the strings.    * @param l a non-null list of strings   ...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

  • Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. *...

    Course.java import java.io.Serializable; /** * Represents a course that might be taken by a student. * */ public class Course implements Serializable { private String prefix; private int number; private String title; private String grade; /** * Constructs the course with the specified information. * * @param prefix the prefix of the course designation * @param number the number of the course designation * @param title the title of the course * @param grade the grade received for the course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT