Advertisement
daniel_lawson9999

Daniel Lawson Chapter 6 Project 2/1

May 10th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. Pseudo Code:
  2. The program compares two text files and compares their text
  3.     The program will first include the java.io.*, and the java.util.*,
  4.     The program will first create a scanner which will be used for user input
  5.     The program will first prompt the user for the name of the first fileit will ask for
  6.     the file name until the user enters a valid file. The program will then create a scanner based off this file.
  7.     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
  8.     afterwards it will create a second scanner for the file, It will then create an additional scanner which will
  9.     be used for reading input of the file a second time
  10.     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
  11.     The program will run a for loop that runs equal to the lines of the larger file
  12.      each time the program will store the next of each file and do nothing if both lines are equal.
  13.      if they lines are unequal, then the program will print them.
  14. ---------------------------------------------------------------------------------------------------------------------------------------
  15. /**
  16.  * Created by Daniel on 5/9/2016.
  17.  */
  18. import java.util.*;
  19. import java.io.*;
  20. public class Project2 {
  21.     public static void main(String[] args)throws FileNotFoundException{
  22.         Scanner s = new Scanner(System.in);// create  a scanner for user input
  23.         System.out.print("Enter a first file name: ");// prompt the user
  24.         File f = new File(s.next());
  25.         while(!f.exists() || !f.canRead()){// prompt until valid input
  26.             System.out.print("Enter a first file name: ");
  27.             f = new File(s.next());
  28.         }
  29.         System.out.print("Enter a second file name: ");// enter a second file name
  30.         File f2 = new File(s.next());
  31.         while(!f2.exists() || !f2.canRead()){//prompt the user until they enter a valid file name
  32.             System.out.print("Enter a first file name: ");
  33.             f2 = new File(s.next());
  34.         }
  35.         // create two sets of scanners for  each document, one scanner is used for the documents length,
  36.         // while the other is for the documents information
  37.         Scanner s1 = new Scanner(f);
  38.         Scanner s2 = new Scanner(f2);
  39.         Scanner ss1 = new Scanner(f);
  40.         Scanner ss2 = new Scanner(f2);
  41.         int maxLines = Math.max(getLines(ss1),getLines(ss2));// figure out the most lines in a file
  42.         String l1 = "";// create strings that will be used to store the temporary value of each line
  43.         String l2 = "";
  44.         System.out.println("Differences Found:");
  45.         for(int i = 1; i <= maxLines;i++){//run for loop that will run for each line of the program
  46.             if(s1.hasNextLine())l1 = s1.nextLine();// create new string for line
  47.             if(s2.hasNextLine())l2 = s2.nextLine();// create new string for other line
  48.             if(!(l1.equals(l2)) ){
  49.                 System.out.printf("line %d:\n",i);// if lines are unique print the differences
  50.                 System.out.printf("<%s\n",l1);
  51.                 System.out.printf(">%s\n",l2);
  52.                 System.out.println();
  53.             }
  54.         }
  55.     }
  56.     public static int getLines(Scanner s){//method finds how many lines are in a program
  57.         int c = 0;
  58.         while(s.hasNextLine()){
  59.             s.nextLine();
  60.             c++;
  61.         }
  62.         return c;
  63.     }
  64. }
  65. ---------------------------------------------------------------------------------------------------------------------------------------
  66. //  I also did number 1
  67. /**
  68.  * Created by Daniel on 5/6/2016.
  69.  */
  70.  
  71. import java.util.*;
  72. import java.io.*;
  73.  
  74. public class project1 {
  75.     public static void main(String[] args) throws FileNotFoundException {
  76.         Scanner s = new Scanner(System.in);//create scanner for user input
  77.         System.out.println("Enter a file name!"); // prompt for file name
  78.         File f = new File(s.next()); //
  79.         while(!f.canRead()){//ask for another file name if the previously given name is invalid
  80.             System.out.println("Enter a valid file, try again!");
  81.             f = new File(s.next());
  82.         }
  83.         Scanner i = new Scanner(f);//create a scanner once file is valid
  84.         //l stores lines, w stores words, c stores characters
  85.         int l = 0;
  86.         int w = 0;
  87.         int c = 0;
  88.         while(i.hasNextLine()){// while there is a new line
  89.  
  90.             l++; // up the line count
  91.             String nl = i.nextLine();// get the next line
  92.             Scanner sl = new Scanner(nl); // create a scanner for it
  93.             while(sl.hasNext()){//while their are next words
  94.                 w++;// up the word cont
  95.                 String word = sl.next();//create a string for the next word
  96.                 c+= word.length();// up the char count (non-space) characters
  97.             }
  98.         }
  99.         System.out.printf("The file contains %d lines, %d words, and %d characters",l,w,c);//print the information about the file
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement