Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pseudo Code:
- The program compares two text files and compares their text
- The program will first include the java.io.*, and the java.util.*,
- The program will first create a scanner which will be used for user input
- The program will first prompt the user for the name of the first fileit will ask for
- the file name until the user enters a valid file. The program will then create a scanner based off this file.
- The program will then prompt the user for a second file, and will continue to ask for a file name until they enter a valid file name
- afterwards it will create a second scanner for the file, It will then create an additional scanner which will
- be used for reading input of the file a second time
- The program will then measure the which file is longer it will do this by calling a method which evaluates the length of a file in lines, and will compare each call to each other
- The program will run a for loop that runs equal to the lines of the larger file
- each time the program will store the next of each file and do nothing if both lines are equal.
- if they lines are unequal, then the program will print them.
- ---------------------------------------------------------------------------------------------------------------------------------------
- /**
- * Created by Daniel on 5/9/2016.
- */
- import java.util.*;
- import java.io.*;
- public class Project2 {
- public static void main(String[] args)throws FileNotFoundException{
- Scanner s = new Scanner(System.in);// create a scanner for user input
- System.out.print("Enter a first file name: ");// prompt the user
- File f = new File(s.next());
- while(!f.exists() || !f.canRead()){// prompt until valid input
- System.out.print("Enter a first file name: ");
- f = new File(s.next());
- }
- System.out.print("Enter a second file name: ");// enter a second file name
- File f2 = new File(s.next());
- while(!f2.exists() || !f2.canRead()){//prompt the user until they enter a valid file name
- System.out.print("Enter a first file name: ");
- f2 = new File(s.next());
- }
- // create two sets of scanners for each document, one scanner is used for the documents length,
- // while the other is for the documents information
- Scanner s1 = new Scanner(f);
- Scanner s2 = new Scanner(f2);
- Scanner ss1 = new Scanner(f);
- Scanner ss2 = new Scanner(f2);
- int maxLines = Math.max(getLines(ss1),getLines(ss2));// figure out the most lines in a file
- String l1 = "";// create strings that will be used to store the temporary value of each line
- String l2 = "";
- System.out.println("Differences Found:");
- for(int i = 1; i <= maxLines;i++){//run for loop that will run for each line of the program
- if(s1.hasNextLine())l1 = s1.nextLine();// create new string for line
- if(s2.hasNextLine())l2 = s2.nextLine();// create new string for other line
- if(!(l1.equals(l2)) ){
- System.out.printf("line %d:\n",i);// if lines are unique print the differences
- System.out.printf("<%s\n",l1);
- System.out.printf(">%s\n",l2);
- System.out.println();
- }
- }
- }
- public static int getLines(Scanner s){//method finds how many lines are in a program
- int c = 0;
- while(s.hasNextLine()){
- s.nextLine();
- c++;
- }
- return c;
- }
- }
- ---------------------------------------------------------------------------------------------------------------------------------------
- // I also did number 1
- /**
- * Created by Daniel on 5/6/2016.
- */
- import java.util.*;
- import java.io.*;
- public class project1 {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner s = new Scanner(System.in);//create scanner for user input
- System.out.println("Enter a file name!"); // prompt for file name
- File f = new File(s.next()); //
- while(!f.canRead()){//ask for another file name if the previously given name is invalid
- System.out.println("Enter a valid file, try again!");
- f = new File(s.next());
- }
- Scanner i = new Scanner(f);//create a scanner once file is valid
- //l stores lines, w stores words, c stores characters
- int l = 0;
- int w = 0;
- int c = 0;
- while(i.hasNextLine()){// while there is a new line
- l++; // up the line count
- String nl = i.nextLine();// get the next line
- Scanner sl = new Scanner(nl); // create a scanner for it
- while(sl.hasNext()){//while their are next words
- w++;// up the word cont
- String word = sl.next();//create a string for the next word
- c+= word.length();// up the char count (non-space) characters
- }
- }
- System.out.printf("The file contains %d lines, %d words, and %d characters",l,w,c);//print the information about the file
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement