Question: I have this Java file with 5 points that need to be filled in (marked with comments that say what to do) and I don't
I have this Java file with 5 points that need to be filled in (marked with comments that say what to do) and I don't know how to do it. The text file it refers to is simply 42 one-liners, some occupying 2 lines, with spaces in between. Also, in the skeleton given, the file and class names do not match. one or both of these must be changed to make the program run. import java.io.File; import java.io.FileNotFoundException; import java.util.Random; import java.util.Scanner; public class OneLiners { final private String[] db; // the one-liners final private int n; // the number of one-liners final static private Random prng = new Random(); public OneLiners(final String fileName) throws FileNotFoundException { final Scanner in = new Scanner(new File(fileName)); db = new String[1024]; n = readDb(in); in.close(); } // constructor private int readDb(final Scanner in) { int index = 0; while (in.hasNextLine()) db[index++] = readEntry(in); return index; } // readDb() private String readEntry(final Scanner in) { StringBuilder sb = new StringBuilder(in.nextLine()); while (in.hasNextLine()) { String line = in.nextLine(); if (line.length() == 0) return sb.toString(); sb.append(' '); sb.append(line); } // while return sb.toString(); } // readEntry() public String get(final int index) { // // Insert code that returns the one-liner from db at the specified // index. // } // get() public int size() { // // Insert code to return the size of db (the number of non-null // elements in the array) // } // size() public static void main(String[] args) throws FileNotFoundException { final OneLiners ol = new OneLiners("one-liners.txt"); final Scanner in = new Scanner(System.in); String cmnd; do { System.out.println(); System.out.print("Enter a command: "); cmnd = in.nextLine().trim().toLowerCase(); if (Character.isDigit(cmnd.charAt(0))) // // Insert code to call stringAtIndex() and to display the result // else if (cmnd.charAt(0) == 'r') // // Insert code to display a randomly-chosen item from the db // else if (cmnd.charAt(0) == 's') // // Insert code to dislay the number of one-liners in the db // } while (cmnd.charAt(0) != 'q'); } // main() public static String stringAtIndex(OneLiners ol, String cmnd) { final int index = Integer.parseInt(cmnd); return ol.get(index); } // stringAtIndex() } // class OneLiners Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
