/* * Class oppgave1a * By Rune Vikestad * E-Mail vikestad@stud.ntnu.no * Web http://folk.ntnu.no/vikestad/ * Description IT-1103, Basic Programming (Java), Exercise 6 */ import java.io.*; import java.util.*; import javax.swing.JOptionPane; /** * Class: 'oppgave1b' */ public class oppgave1b { /** * Method: 'main'. */ public static void main(String[] args) throws IOException { /* * Variable Declarations */ FileReader fr; FileWriter fw; PrintWriter pw; String fileInName; // The name of the file we read from String fileOutName; // The name of the file we write to int charCode; // The decimal value of a character read char wordPart; // The alphanumeric value of a character read. String wordTemp = ""; // The temporary string used to store words, before they're processed into an array. int counter = 0; // We use this integer to count how many words we've read, for the array. String[] wordArray; // Our words, in an array. /* * Variable Initiations */ fileInName = JOptionPane.showInputDialog("File name: "); fileOutName = JOptionPane.showInputDialog("File name (Output): "); fr = new FileReader(fileInName); fw = new FileWriter(fileOutName); pw = new PrintWriter(fw); wordArray = new String[100]; /* * Process our stream. */ do { charCode = fr.read(); /* If character isn't a Comma, Period, Space or EOF. */ if (charCode != 46 && charCode != 44 && charCode != 32 && charCode != -1) { if (charCode >= 65 && charCode < 97) { /* Found an upper case character, so make it lower case. */ charCode = charCode + 32; } /* Insert the character into our temporary word */ wordPart = (char)charCode; wordTemp = wordTemp + wordPart; } if (charCode == 32) { /* We found a space, which means we've got a word. */ wordArray[counter] = wordTemp; System.out.println(wordArray[counter]); // Print to screen. pw.println(wordArray[counter]); // Print to file. wordTemp = ""; if (counter == 0) { counter = counter+1; } else if (counter > 0) { counter = counter+1; } } else if (charCode == -1) { /* We found the EOF, print the last word. */ wordArray[counter] = wordTemp; System.out.println(wordArray[counter]); // Print to screen. pw.println(wordArray[counter]); // Print to file. wordTemp = ""; if (counter == 0) { counter = counter+1; } else if (counter > 0) { counter = counter+1; } } } while (charCode > 0); fr.close(); fw.close(); pw.close(); System.exit(0); } }