Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;  
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.Arrays;
  7.  
  8.  
  9. public class Task1abc {
  10.  
  11.  public static void main (String[] args) throws IOException { //throw up an exception error if something goes wrong
  12.      
  13.     Path sonnet = Paths.get(System.getProperty("user.dir")+"/sonnet1-a.txt"); // Sets path to file and allows it to be opened anywhere)
  14.      
  15.     BufferedReader reader = null;
  16.    
  17.      int totalWords = 0;
  18.      int totalLetters = 0;
  19.      int totalLines = 0;
  20.      int even = 0;
  21.      int odd = 0;
  22.      boolean t = true;
  23.      String[]totalEvenWords = new String[0];
  24.      String[]totalOddWords = new String[0];
  25.      String thisWord = " ";
  26.      
  27. {
  28.     reader = new BufferedReader(new FileReader("sonnet1-a.txt"));
  29.  
  30.        
  31. //Get number of words and letters        
  32.     String thisLine = reader.readLine();
  33.         totalLines++;
  34.    
  35.     String[] words = thisLine.split(" ");
  36.     totalWords += words.length;
  37.    
  38.     for  (String word:words)
  39.     {
  40.         totalLetters += word.length();
  41.         thisLine = reader.readLine();
  42.        
  43.     }
  44.      
  45.      
  46. //Check length of every word and assign it to either even or odd + translate all to upper case
  47. if (thisWord.length() % 2 == 0){
  48.  
  49.     for (String evens : totalEvenWords) {
  50.         if(evens.equals(thisWord.toUpperCase())){
  51.         t = false;
  52.         break;
  53.         }
  54.     }
  55.     if (t){
  56.     totalEvenWords = Arrays.copyOf(totalEvenWords,totalEvenWords.length +1);
  57.     totalEvenWords[totalEvenWords.length - 1] = thisWord.toUpperCase();                                      
  58.     }
  59.     even++;
  60.     }
  61.     else {
  62.    
  63.      for (String odds : totalOddWords) {
  64.         if(odds.equals(thisWord.toUpperCase())){
  65.         t = false;
  66.         break;
  67.         }
  68.     }
  69.     if (t){
  70.     totalOddWords = Arrays.copyOf(totalOddWords,totalOddWords.length +1);
  71.     totalOddWords[totalOddWords.length - 1] = thisWord.toUpperCase();                        }
  72.     }                        
  73.     odd++;
  74.  }
  75.    
  76.  
  77.    
  78.     Arrays.sort(totalEvenWords);
  79.     Arrays.sort(totalOddWords);
  80.    
  81.    
  82.  
  83.    
  84.        
  85. System.out.println("File analysed: sonnet1-a.txt");
  86. System.out.println("There are "+totalWords+" words and "+totalLetters+" letters");
  87. System.out.println("There are " +even+ " even words and " +odd+ " odd words");
  88. System.out.println("Even set:"+Arrays.toString(totalEvenWords));
  89. System.out.println("Odd set:"+Arrays.toString(totalOddWords));
  90.    
  91.    
  92.      
  93.  
  94. try {
  95.     reader.close();           //Closing the reader
  96. }
  97.     catch (IOException e) {
  98.     System.out.println("File I/O error");
  99.         }
  100.     }    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement