Question

In: Computer Science

Write a Java program that uses printf, and takes user input to find the name of...

Write a Java program that uses printf, and takes user input to find the name of the File.

  1. FileCompare (20)

Write a program that compares to files line by line, and counts the number of lines that are different. You can use file1.txt and file2.txt when testing your program, but you must ask for the file names in the input.

main

Interactively requests the names of the two files, after creating the Scanner for the keyboard.

Creates a Scanner for each of the two files.

Calls countDifferentLines with references to the two Scanners as parameters and accepts an integer return value.

Prints the result (see sample).

countDifferentLines

Accepts references to the two Scanners as parameters

Compares the two files line by line – They may have a different number of lines

Return the number of lines that are different

Sample output

Please enter name of the first file to compare: file1.txt

Please enter name of the second file to compare: file2.txt

The files differ in 4 line(s)

Solutions

Expert Solution

import java.io.File;
import java.util.Scanner;

public class FileCompare {
   public static void main(String[] args) throws Exception {
       Scanner sc = new Scanner(System.in);
       //reading file names
       System.out.println("Enter name of the first file to compare: ");
       String f1 = sc.next();
       System.out.println("Enter name of the second file to compare: ");
       String f2 = sc.next();

       int c=countDifferentLines(new Scanner(new File(f1)), new Scanner(new File(f2)));
       System.out.println("The files differ in "+c+" line(s)");
   }

   private static int countDifferentLines(Scanner sc1, Scanner sc2) throws Exception {
       int count = 0;
       String l1 = "", l2 = "";
       //looping the files
       while (sc1.hasNext() || sc2.hasNext()) {
           //reading lines
           if(sc1.hasNext())
           l1 = sc1.nextLine();
           if(sc2.hasNext())
           l2 = sc2.nextLine();
           //checking the difference
           if (l1 == null && l2 == null)
               return count;
           if (l1 == null && l2 != null)
               count++;
           else if (l1 != null && l2 == null)
               count++;
           else if (!l1.equals(l2))
               count++;

       }
       //returns count
       return count;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

This program must be in java, use printf, and request user input for a file name....
This program must be in java, use printf, and request user input for a file name. NameSubsLast3 (30 points) Write a program that does the following. Write a program that does the following. Requests the name of an input file and uses it to create an input file Scanner. Requests the name of an output file and uses it to create a PrintWriter. I have posted firstNames.txt and outNames.txt in the homework for Lesson 6. Calls createArray to create a...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Question 1: Write a Java program that prompts the user to input a file name (existing...
Question 1: Write a Java program that prompts the user to input a file name (existing text file), then calculate and display the numbers of lines in that file. Also calculate and display the length of the longest line in that file. For example, if the input file has the following lines: Hello This is the longest line Bye The output should be: The file has 3 lines. The longest line is line 2 and it has 24 characters. Test...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a spell checking program (java) which uses a dictionary of words (input by the user...
Write a spell checking program (java) which uses a dictionary of words (input by the user as a string) to find misspelled words in a second string, the test string. Your program should prompt the user for the input string and the dictionary string. A valid dictionary string contains an alphabetized list of words. Functional requirements: For each word in the input string, your program should search the dictionary string for the given word. If the word is not in...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT