SlideShare a Scribd company logo
1 of 4
Download to read offline
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");
}
}
}
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;
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)
{
// 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

More Related Content

Similar to Write a program that asks the user for the name of a file. The progr.pdf

Similar to Write a program that asks the user for the name of a file. The progr.pdf (20)

Java IO
Java IOJava IO
Java IO
 
Linux basics
Linux basicsLinux basics
Linux basics
 
02 fundamentals
02 fundamentals02 fundamentals
02 fundamentals
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling
File handlingFile handling
File handling
 
Operating system
Operating systemOperating system
Operating system
 
Project report
Project reportProject report
Project report
 
IN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdfIN C++ languageWrite a simple word processor that will accept text.pdf
IN C++ languageWrite a simple word processor that will accept text.pdf
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
File
FileFile
File
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2
 

More from arri2009av

Identify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdfIdentify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdfarri2009av
 
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdfIdentify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdfarri2009av
 
From a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdfFrom a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdfarri2009av
 
Explain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdfExplain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdfarri2009av
 
Explain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdfExplain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdfarri2009av
 
Einstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdfEinstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdfarri2009av
 
Contrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdfContrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdfarri2009av
 
Based on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdfBased on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdfarri2009av
 
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdfBlair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdfarri2009av
 
An attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdfAn attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdfarri2009av
 
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdfConsider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdfarri2009av
 
A vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdfA vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdfarri2009av
 
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
Assume real numbers R for now. Consider relation  on R, x  y iff x  .pdfAssume real numbers R for now. Consider relation  on R, x  y iff x  .pdf
Assume real numbers R for now. Consider relation on R, x y iff x .pdfarri2009av
 
An enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdfAn enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdfarri2009av
 
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdfarri2009av
 
1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdf1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdfarri2009av
 
1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdf1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdfarri2009av
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdfWings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdfarri2009av
 
Why do financial assets show up as component of household wealth.pdf
Why do financial assets show up as component of household wealth.pdfWhy do financial assets show up as component of household wealth.pdf
Why do financial assets show up as component of household wealth.pdfarri2009av
 

More from arri2009av (20)

Identify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdfIdentify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdf
 
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdfIdentify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
 
From a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdfFrom a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdf
 
Explain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdfExplain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdf
 
Explain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdfExplain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdf
 
Einstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdfEinstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdf
 
Contrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdfContrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdf
 
Based on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdfBased on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdf
 
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdfBlair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
 
An attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdfAn attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdf
 
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdfConsider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
 
A vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdfA vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdf
 
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
Assume real numbers R for now. Consider relation  on R, x  y iff x  .pdfAssume real numbers R for now. Consider relation  on R, x  y iff x  .pdf
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
 
An enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdfAn enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdf
 
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
 
1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdf1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdf
 
1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdf1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdf
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdfWings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
 
Why do financial assets show up as component of household wealth.pdf
Why do financial assets show up as component of household wealth.pdfWhy do financial assets show up as component of household wealth.pdf
Why do financial assets show up as component of household wealth.pdf
 

Recently uploaded

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 

Recently uploaded (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
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