Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a Java program that is not able to read the input from the file: 1. Allows you to search the authors names in

I have a Java program that is not able to read the input from the file:

1. Allows you to search the authors names in the text file below.

2. I then needs to print out a random quote associated with that name searched. So if you searched Donald Knuth it would print a random quote of his.

3. If the name is not found it just prints out a random quote from the input file.

Code

public class Main {

/** * Finds number of lines in a file * @param file * @return number of lines in a file */

//parse file to a dictionary of authors and their quotes static Dictionary> ParseFile(String fileName) throws IOException { Dictionary> AuthorQuotes = new Hashtable>(); BufferedReader bf = new BufferedReader(new FileReader(fileName)); String line = bf.readLine(); String quote = ""; while (line != null) { if(!line.isEmpty()){ if(line.startsWith("; ")){//if line contains author name String author = line.replace("; ", ""); if(AuthorQuotes.get(author) == null){ AuthorQuotes.put(author, new ArrayList()); } ArrayList quotes = (ArrayList) AuthorQuotes.get(author); quotes.add(quote); quote = ""; } else//else if it contains quote quote += line; } line = bf.readLine();//reading file line by line } bf.close(); return AuthorQuotes; } public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); System.out.print("Enter file name: "); String fileName = scanner.nextLine(); Dictionary> AuthorQuotes = ParseFile(fileName);//parsing file boolean exit = false; while(!exit) { System.out.print("Enter the author name you want to search: "); String author = scanner.nextLine();//inputting author name to search if(AuthorQuotes.get(author) == null){//if author not found System.out.println("Author \"" + author + "\" not found in file " + fileName); } else//if author found { System.out.println("Author \"" + author + "\" found in file " + fileName); ArrayList quotes = AuthorQuotes.get(author); String RandomQuote = quotes.get((new Random()).nextInt(quotes.size()));//gets random quote System.out.println("Printing one of " + author + "'s random quote: " + RandomQuote); } System.out.print("Exit(y/n): "); exit = (scanner.nextLine().toLowerCase().startsWith("y")); } scanner.close();

}

}

The text file for the program

Premature optimization is the root of all evil (or at least most of it) in programming.;Donald Knuth 
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.;Donald Knuth
Lisp has jokingly been called "the most intelligent way to misuse a computer". I think that description is a great compliment because it transmits the full flavor of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts.;Edsger Dijkstra, CACM, 15:10 Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great.;Mark Twain
If you tell the truth, you don't have to remember anything.;Mark Twain
Life is what happens to you while you're busy making other plans.;John Lennon Whenever you find yourself on the side of the majority, it is time to pause and reflect.;Mark Twain

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

1. Signs and symbols of the map Briefly by box ?

Answered: 1 week ago

Question

Types of physical Maps?

Answered: 1 week ago

Question

Explain Intermediate term financing in detail.

Answered: 1 week ago

Question

Types of cultural maps ?

Answered: 1 week ago

Question

=+ a. a family deciding whether to buy a new car

Answered: 1 week ago

Question

=+10. How are inflation and unemployment related in the short run?

Answered: 1 week ago