Here is my solution to lab 2:
import java.io.*;
import java.util.*;
public class BioPrinter {
public static void main(String[] args) {
char [][][] alanText = getText();
int totalLineCount = 0;
int totalWordCount = 0;
int totalCharacterCount = 0;
int longestLineLength = 0;
int longestLineIndex = 0;
int longestWordLength = 0;
int longestWordLineIndex = 0;
int longestWordWordIndex = 0;
// Part 1 and part 2 mixed
totalLineCount = alanText.length;
for (int currentLineIndex = 0;
currentLineIndex < totalLineCount;
currentLineIndex ++)
{
int currentWordCount = (alanText[currentLineIndex]).length;
totalWordCount += currentWordCount;
// Part 2 (Just the if)
if (currentWordCount > longestLineLength)
{
longestLineLength = currentWordCount;
longestLineIndex = currentLineIndex;
}
for (int currentWordIndex = 0;
currentWordIndex < currentWordCount;
currentWordIndex ++)
{
int currentCharacterCount =
(alanText[currentLineIndex][currentWordIndex]).length;
totalCharacterCount += currentCharacterCount;
// Part 2 (Just the if)
if (currentCharacterCount > longestWordLength)
{
longestWordLength = currentCharacterCount;
longestWordLineIndex = currentLineIndex;
longestWordWordIndex = currentWordIndex;
}
}
}
System.out.println("=== PART 1 ===");
System.out.println(" Alan has " + totalLineCount + " lines, " +
totalWordCount + " words, and " +
totalCharacterCount + " characters");
// End part 1
// Part 2
System.out.println();
System.out.println("=== PART 2 ===");
System.out.println(" Longest line is at index " +
longestLineIndex + " and reads:");
System.out.print(" ");
for (int currentWordIndex = 0;
currentWordIndex < longestLineLength;
currentWordIndex ++)
{
System.out.print(new String(alanText[longestLineIndex][currentWordIndex]));
System.out.print(" ");
}
System.out.println();
System.out.println(" Longest word is at line index " + longestWordLineIndex +
" and word index " + longestWordWordIndex + " and reads:");
System.out.println(
" " + new String(alanText[longestWordLineIndex][longestWordWordIndex]));
// End part 2
// Part 3
System.out.println();
System.out.println("=== PART 3 ===");
final int MAX_WORD_LENGTH = 20;
for (int currentWordLength = 1;
currentWordLength < MAX_WORD_LENGTH;
currentWordLength ++)
{
System.out.print(" " + currentWordLength + ": ");
for (int currentLineIndex = 0;
currentLineIndex < totalLineCount;
currentLineIndex ++)
{
int currentWordCount = alanText[currentLineIndex].length;
for (int currentWordIndex = 0;
currentWordIndex < currentWordCount;
currentWordIndex ++)
{
if (alanText[currentLineIndex][currentWordIndex].length ==
currentWordLength)
{
System.out.print(
new String(alanText[currentLineIndex][currentWordIndex]) +
" ");
}
}
}
System.out.println();
}
// End part 3
}
/*
* DO NOT EDIT BELOW THIS LINE
*/
private static char[][][] getText() {
Vector lines = new Vector();
try {
String nextLine = null;
BufferedReader in =
new BufferedReader(new FileReader("Alan.txt"));
while ((nextLine = in.readLine()) != null) {
String[] words = nextLine.split(" ");
Vector wordVec = new Vector();
for (int i = 0; i < words.length; i++) {
wordVec.add(words[i].toCharArray());
}
lines.add(wordVec.toArray(new char[0][0]));
}
} catch (IOException ioe) {
System.err.println("I/O Error: " + ioe);
System.exit(1);
}
return (char[][][]) lines.toArray(new char[0][0][0]);
}
}