Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Arrays;
- public class Task1abc {
- public static void main (String[] args) throws IOException { //throw up an exception error if something goes wrong
- Path sonnet = Paths.get(System.getProperty("user.dir")+"/sonnet1-a.txt"); // Sets path to file and allows it to be opened anywhere)
- BufferedReader reader = null;
- int totalWords = 0;
- int totalLetters = 0;
- int totalLines = 0;
- int even = 0;
- int odd = 0;
- boolean t = true;
- String[]totalEvenWords = new String[0];
- String[]totalOddWords = new String[0];
- String thisWord = " ";
- {
- reader = new BufferedReader(new FileReader("sonnet1-a.txt"));
- //Get number of words and letters
- String thisLine = reader.readLine();
- totalLines++;
- String[] words = thisLine.split(" ");
- totalWords += words.length;
- for (String word:words)
- {
- totalLetters += word.length();
- thisLine = reader.readLine();
- }
- //Check length of every word and assign it to either even or odd + translate all to upper case
- if (thisWord.length() % 2 == 0){
- for (String evens : totalEvenWords) {
- if(evens.equals(thisWord.toUpperCase())){
- t = false;
- break;
- }
- }
- if (t){
- totalEvenWords = Arrays.copyOf(totalEvenWords,totalEvenWords.length +1);
- totalEvenWords[totalEvenWords.length - 1] = thisWord.toUpperCase();
- }
- even++;
- }
- else {
- for (String odds : totalOddWords) {
- if(odds.equals(thisWord.toUpperCase())){
- t = false;
- break;
- }
- }
- if (t){
- totalOddWords = Arrays.copyOf(totalOddWords,totalOddWords.length +1);
- totalOddWords[totalOddWords.length - 1] = thisWord.toUpperCase(); }
- }
- odd++;
- }
- Arrays.sort(totalEvenWords);
- Arrays.sort(totalOddWords);
- System.out.println("File analysed: sonnet1-a.txt");
- System.out.println("There are "+totalWords+" words and "+totalLetters+" letters");
- System.out.println("There are " +even+ " even words and " +odd+ " odd words");
- System.out.println("Even set:"+Arrays.toString(totalEvenWords));
- System.out.println("Odd set:"+Arrays.toString(totalOddWords));
- try {
- reader.close(); //Closing the reader
- }
- catch (IOException e) {
- System.out.println("File I/O error");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement