Question

In: Computer Science

Before you begin your program: Create a text file in your project folder with 20 "quirky...

Before you begin your program: Create a text file in your project folder with 20 "quirky sayings"/fortunes (the only requirement is that they be appropriate for display in class), In your program you will Create a list by reading those 20 fortunes from your file Ask the user how many fortunes he/she wants to see. store the value in a variable Inside of a loop (loop as many times as the user asked) select a random answer from your list of fortunes wait for the user to press the enter key (the easy way is to use the Scanner's nextLine method) Note that it is quite possible that you will see some fortunes more than once while not seeing some at all.

Solutions

Expert Solution

If you have any problem with the program feel free to comment.

Program

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Test {
   public static void main(String[] args)throws IOException {
       Scanner sc = new Scanner(System.in);//for taking console input
      
       //for reading file data
       BufferedReader br = new BufferedReader(new FileReader("data.txt"));
       String line;
       int count=0;
       String[] fortunes = new String[20];
       //reading and storing file data to fortunes[]
       while ((line = br.readLine()) != null) {
           fortunes[count] = line;
           count++;
       }
      
       System.out.print("Enter the number of fortunes: ");
       int size = sc.nextInt(); sc.nextLine();//taking user input and flushing scanner object
       int random;
       System.out.println("*********Your Fortunes*********");
       for(int i=0; i<size; i++) {
           random = (int)(Math.random()*20);//generating random value
           System.out.println(fortunes[random]);//showing the fortunes
           sc.nextLine();//waiting for user to press enter
       }
       //closing the resources
       sc.close();
       br.close();
   }
}

data.txt

Output


Related Solutions

language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes...
language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes (the only requirement is that they be appropriate for display in class), If I use my own file though, you should handle as many fortunes as I put in. Make each fortune its own line, •in your main function ask the user for the name of the fortunes file.•Create a function which takes the name of the fortunes file as a parameter, open that...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
In the root of the project create a folder called res. In that folder create a...
In the root of the project create a folder called res. In that folder create a file called DemoFile.txt Create a class with a static main that tests the ability to resolve and print a Path: Create an instance of a FileSystem class. Create an instance of the Path interface for the DemoFile.txt file. Print the constructed Path with System.out.println() method. Create a class that does the following: Using a pre-Java 7 solution, create a class that tests streams in...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
Python File program 5: Word Frequencies (Concordance)    20 pts 1. Use a text editor to create...
Python File program 5: Word Frequencies (Concordance)    20 pts 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE:...
C program! Create a list of 5 things-to-do when you are bored (in the text file...
C program! Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading things in,...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder....
Using NetBeans, create a Java project named FruitBasket. Set the project location to your own folder. 3. Import Scanner and Stacks from the java.util package. 4. Create a Stack object named basket. 5. The output shall: 5.1.Ask the user to input the number of fruits s/he would like to catch. 5.2.Ask the user to choose a fruit to catch by pressing A for apple, O for orange, M for mango, or G for guava. 5.3.Display all the fruits that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT