I have to search a word in a file and count how many words in and how long takes to find it. My code working well but counts the empty lines as words and I don't want the empty lines to be counted . would you look at it and see what I can add to it so the code will not count the empty lines in the file.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I have this homework where I have to search a word in a file and count how many words in and how long takes to find it. My code working well but counts the empty lines as words and I don't want the empty lines to be counted . would you look at it and see what I can add to it so the code will not count the empty lines in the file. 

Here the code 

import java.io.File;
import java.io.FileNotFoundException;
//scanner class for user input
import java.util.Scanner;
public class Uppgift2 {
//main function
public static void main(String[] args) {
//string to store input file name
String input_file;
//check if argument is passed in command line
if (args.length == 0) {
//if arg. is not passed initialise file to wordlist1.txt
input_file = "wordlist1.txt";
} else {
//else store the argument
input_file = args[0];
}
try {
//opening file in read mode
File fp = new File(input_file);
//scanner object for input file
Scanner scan = new Scanner(fp);
//scanner object to take word input
Scanner search = new Scanner(System.in);
String word, current;
System.out.print("Enter the word to search for>");
//read the word to search
word = search.nextLine();
//count for total number of words
int count = 0;
//variable to track if word is present or not
boolean found = false;
//store the starting time
long start = System.nanoTime();
//loop for reading all words in file
while (scan.hasNextLine()) {
//reading the current word
current = scan.nextLine();
//check if current contains the word to search
if (current.contains(word)) {
found = true;
}
//increasing count for no. of words
count++;
}
//close the input file
scan.close();
//store the end time
long end = System.nanoTime();
//print the count of words
System.out.println("\nThe file '" + input_file + "' contains " + count + " words");
//print whether the word is present or not
if (found) {
System.out.println("The word '" + word + "' exists in this file.");
} else {
System.out.println("The word '" + word + "' does not exists in this file.");
}
//calculating the time to search in ms
long time = end - start;
time = time / 1000000;
System.out.println("The search took " + time + " ms");
} catch (Exception e) {
e.printStackTrace();
}

}
}

Exempel
c:\>java labb2
Enter the word to search for>hello
The file 'wordlistl.txt' contains 55899 words
The word 'hello' exists in this file
The search took 108 ms
c:\>java labb2 wordlist2.txt
Enter the word to search for>hello
The file 'wordlist2.txt' contains l09583 words
The word 'hello' exists in this file
The search took 169 ms
c:\>java labb2 wordlist3.txt
Enter the word to search for>hello
The file 'wordlist3.txt' contains 354986 words
The word 'hello' exists in this file
The search took 296 ms
c:\>java labb2
Enter the word to search for>xyz
The file 'wordlistl.txt' contains 55899 words
The word 'xyz' does not exists in this file
The search took 116 ms
c:\>java labb2 Demo.txt
Enter the word to search for>java
Exception in thread "main" java ieFileNotFeundException: finns-
inte.txt (We don't find this file)
at java base/java.io.FileInputStream.open0 (Native Method)
at
java baseLiavaie.filelngutstream.2pen(FileInputStream.java:216)
at
java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java baseLiava.util:Scanner-Sini> (Scanner.java:639)
at labb2.main(labb2.java:21)
Transcribed Image Text:Exempel c:\>java labb2 Enter the word to search for>hello The file 'wordlistl.txt' contains 55899 words The word 'hello' exists in this file The search took 108 ms c:\>java labb2 wordlist2.txt Enter the word to search for>hello The file 'wordlist2.txt' contains l09583 words The word 'hello' exists in this file The search took 169 ms c:\>java labb2 wordlist3.txt Enter the word to search for>hello The file 'wordlist3.txt' contains 354986 words The word 'hello' exists in this file The search took 296 ms c:\>java labb2 Enter the word to search for>xyz The file 'wordlistl.txt' contains 55899 words The word 'xyz' does not exists in this file The search took 116 ms c:\>java labb2 Demo.txt Enter the word to search for>java Exception in thread "main" java ieFileNotFeundException: finns- inte.txt (We don't find this file) at java base/java.io.FileInputStream.open0 (Native Method) at java baseLiavaie.filelngutstream.2pen(FileInputStream.java:216) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) at java baseLiava.util:Scanner-Sini> (Scanner.java:639) at labb2.main(labb2.java:21)
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
InputStream
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education