I need to print out each letter from a text file using a list. I'm not sure where it is currently messing up.
Here is the code that I have so far. It currently only prints out the first letter from the file. For instance if the first character was "H" it would just print out the "H" and not continue with the rest of the file. I tested with multiple files to make sure it wasn't just the file I was working with. It takes the phrase from a standard .txt file.
Any help would be greatly appreciated!
package parsephrase;
/**
*
* @author Matt
*/
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
public class ParsePhrase {
private final Path filePath;
private ArrayList<String> inputList = new ArrayList<String>();
private ArrayList<Character>outputList = new ArrayList<Character>();
public ParsePhrase(String inputFile) {
filePath = Paths.get(inputFile);
}
public void readLines() {
try (Scanner input = new Scanner(filePath)) {
while (input.hasNextLine()) {
logInputLine(input.nextLine());
}
input.close();
}
catch (IOException e) {
System.out.println("Unable to access file.");
}
}
public void logInputLine(String lineIn) {
Scanner input = new Scanner(lineIn);
inputList.add(input.nextLine());
input.close();
}
public void displayOutput() {
for (int inputListIndex = 0; inputListIndex < inputList.size(); inputListIndex++) {
String inputString = inputList.get(inputListIndex);
for (int inputStringIndex = 0; inputStringIndex < inputString.length(); inputStringIndex++) {
if (outputList.isEmpty()) {
outputList.add(inputString.charAt(inputStringIndex));
continue;
}
for (int outputListIndex = 0; outputListIndex < outputList.size(); outputListIndex++) {
if (Character.isLetter(inputString.charAt(inputStringIndex))) {
if (inputString.charAt(inputStringIndex) <= outputList.get(outputListIndex));
displayCharArray(outputList);
break;
}
else if (inputString.charAt(inputStringIndex) > outputList.get(outputListIndex)) {
if (outputListIndex == outputList.size() - 1) {
outputList.add(inputString.charAt(inputStringIndex));
displayCharArray(outputList);
break;
} else {
continue;
}
}
}
}
}
}
public void displayCharArray(ArrayList<Character> listIn) {
for (Character c : listIn) {
System.out.println(c);
}
System.out.println();
}
public static void main(String[] args)throws IOException {
ParsePhrase parser = new ParsePhrase("C:\\devel\\cis210\\Week 3\\Test.txt");
parser.readLines();
parser.displayOutput();
}
}