Question

In: Computer Science

Modify the processFile method so that it will compile and it will not produce a runtime...

Modify the processFile method so that it will compile and it will not produce a runtime error: public static void processFile(File file) { File input = "contents.txt"; String line = null; try { input = new File(file); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (file != null) { file.close(); } } }

Solutions

Expert Solution

public static void processFile(File file) {
        BufferedReader input = null;
        String line = null;
        try {
            input = new BufferedReader(new FileReader(file));
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException ex) {
            System.out.println("File not present");
        } catch (IOException ex) {
            System.out.println("Cannot read from file");
        } catch (Exception ex){
                    System.out.println("Somthing went wrong");
        } finally {
            if (file != null) {
                try {
                    input.close();
                } catch (IOException ex) {
                    System.out.println("File cannot be closed");
                } catch (Exception ex){
                    System.out.println("Somthing went wrong");
                }
            }
        }
    }


Related Solutions

Modify the method so that the item with the highest key is not only returned by...
Modify the method so that the item with the highest key is not only returned by the method but is also removed from the array. Call the method removeMax(). // highArray.java // demonstrates array class with high-level interface // to run this program: C>java HighArrayApp //////////////////////////////////////////////////////////////// class HighArray { private long[] a; // ref to array a private int nElems; // number of data items //----------------------------------------------------------- public HighArray(int max) // constructor { a = new long[max]; // create the array...
Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number...
Modify the insertionSort() method in insertSort.java so it counts the number of copies and the number of comparisons it makes during a sort and displays the totals. To count comparisons, you will need to break up the double condition in the inner while loop. Use this program to measure the number of copies and comparisons for different amounts of inversely sorted data. Do the results verify O(N2 ) efficiency? Do the same for almost-sorted data (only a few items out...
Is it ethical to modify genes and produce GMO's?
Is it ethical to modify genes and produce GMO's?
Is it ethical to modify genes and produce GMO's?
Is it ethical to modify genes and produce GMO's?
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right)...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right) element as the pivot, rather than an arbitrary number. (This is similar to what happens in the quickSort1.java program in Listing 7.3.) Make sure your routine will work for arrays of three or fewer elements. To do so, you may need a few extra statements. // partition.java // demonstrates partitioning an array // to run this program: C>java PartitionApp //////////////////////////////////////////////////////////////// class ArrayPar { private...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and uses a toString method to print out it’s instance variables. Make sure toString method cannot be overridden.​​​​​​ Source code below: public abstract class Employee10A {    private String firstName, lastName; static int counter = 0;    public Employee10A(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }    @Override public String toString() { return ("The employee's full name is " + firstName...
Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method...
Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method so that it informs the player a mistake has been made, then display the challenge sentence followed by the player’s sentence so the player can determine where the error lies. #-------------------------------------------------------------------------- # # Script Name: TypingChallenge.rb # Version: 1.0 # Author: Jerry Lee Ford, Jr. # Date: March 2010 # # Description: This Ruby script demonstrates how to apply conditional logic # in order...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the class's capabilities in a simple program. this is what needs to be modified // Specification file for the FeetInches class #ifndef FEETINCHES_H #define FEETINCHES_H #include <iostream> using namespace std; class FeetInches; // Forward Declaration // Function Prototypes for Overloaded Stream Operators ostream &operator << (ostream &, const FeetInches &); istream &operator >> (istream &, FeetInches &); // The FeetInches class holds distances or measurements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT