The document describes a Java program that prompts the user for a file name, checks if the file exists, and counts the number of words in the file. It provides sample input files with text, and code to open each file, split the contents on whitespace, and count the words. The code outputs the number of words in each valid file.
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Write a program that asks the user for the name of a file. The progr.pdf
1. Write a program that asks the user for the name of a file. The program should display the number
of words that the file contains.
Input Validation:
Make sure the file exists, before proceeding. Let the user type quit in order to exit the program. If
the file is empty, the word count should be 0.
This is also in JAVA
Beginner level
Thanks!!!
_____________________________________________________________________________
_____________________________
the files for the reading from file are "input1.txt", "input2.txt", "input3.txt", "input4.txt"
package wordscount;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.io.IOException;
import java.io.*;
public class WordsCount {
public static void main(String[] args) throws FileNotFoundException
{
System.out.println("Please enter the file name or type QUIT to exit: ");
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
if (userInput.equalsIgnoreCase("input1.txt"))
{
try
{
x = new Scanner (new File("input1.txt"));
}
catch (Exception e){
System.out.println("12345");
}
}
}
2. public void readFile(){
while(x.hasNext()){
String a = x.next();
}
}
}
thats what i have so far, i dont know what to do....
Solution
input1.txt (Save this file under D ://Drive then the path of the file pointing to it is D://input1.txt )
Plants, also called green plants, are multicellular eukaryotes of the kingdom Plantae. They form
an unranked clade Viridiplantae (Latin for green plants) that includes the flowering plants,
conifers and other gymnosperms, ferns, clubmosses, hornworts, liverworts, mosses and the green
algae.
__________________
input2.txt (Save this file under D ://Drive then the path of the file pointing to it is D://input2.txt )
Animals are multicellular, eukaryotic organisms of the kingdom Animalia (also called Metazoa).
All animals are motile, meaning they can move spontaneously and independently at some point
in their lives.
__________________
input3.txt (Save this file under D ://Drive then the path of the file pointing to it is D://input3.txt )
The spread of humans and their large and increasing population has had a profound impact on
large areas of the environment and millions of native species worldwide. Advantages that explain
this evolutionary success include a relatively larger brain with a particularly well-developed
neocortex, prefrontal cortex and temporal lobes, which enable high levels of abstract reasoning,
language, problem solving, sociality, and culture through social learning.
______________________________
input4.txt (Save this file under D ://Drive then the path of the file pointing to it is D://input4.txt )
A computer is an electronic device that manipulates information, or data. It has the ability to
store, retrieve, and process data. You may already know that you can use a computer to type
documents, send email, play games, and browse the Web.
____________________________
CountingNoOfLinesInFile.java
import java.io.File;
3. import java.io.IOException;
import java.util.Scanner;
public class CountingNoOfLinesInFile {
public static void main(String args[]) {
// Declaring variables
int count_words = 0;
int no_of_words ;
// Declaring a String array initialized with files
String fname[] = { "D://input1.txt", "D://input2.txt","D://input3.txt", "D://input4.txt"
};
// Creating the Scanner class reference
Scanner sc = null;
//This loop will open the file and count no of words in each file
for (int i = 0; i < fname.length; i++)
{
//Checking whether the file exists or not in the special file path
if(new File(fname[i]).exists())
{
//setting no_of_words variable to zero
no_of_words=0;
try
{
// Opening the file
sc = new Scanner(new File(fname[i]));
// This loops continue to execute until the end of the file
while (sc.hasNext())
{
//Parsing the string into String array
String arr[] = sc.nextLine().split(" ");
//Counting the no of words in the file
no_of_words += arr.length;
}
} catch (IOException e)
4. {
// Displaying the exception
System.out.println("Exception :" + e);
}
// Displaying the number of words in each file
System.out.println("No of words in the File (" + fname[i] + ") is :"+ no_of_words);
}
else
{
//If file not found display the error message
System.out.println(":: "+fname[i]+" File Not Found ::");
}
}
}
}
__________________________
Output:
No of words in the File (D://input1.txt) is :40
No of words in the File (D://input2.txt) is :29
No of words in the File (D://input3.txt) is :64
No of words in the File (D://input4.txt) is :42
___________________
Output2: (If any of the file is not found in the specified path)
No of words in the File (D://input1.txt) is :40
No of words in the File (D://input2.txt) is :29
No of words in the File (D://input3.txt) is :64
:: D://input4.txt File Not Found ::
______Thank You