-1

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();
    }
    }
  • 2
    Welcome to Stack Overflow! You have posted way to much code in your question, which makes it unclear to us (and to future readers) exactly where the problem is. Please reduce your problem code to 10 lines or less. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Sep 16 '17 at 18:01
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Sep 16 '17 at 18:11

1 Answers1

0

Though not tested, yet the issue could be caused by an if condition that is currently resulting being redundant :

if (inputString.charAt(inputStringIndex) <= outputList.get(outputListIndex)); // if terminated by semi colon
displayCharArray(outputList); // display the first character
break; // executed certainly

should ideally be :

if (inputString.charAt(inputStringIndex) <= outputList.get(outputListIndex)) {
    displayCharArray(outputList);
    break;
}
Naman
  • 27,789
  • 26
  • 218
  • 353
  • Thank you for the help. I've modified my code with what you suggested. Unfortunately, I'm still having trouble with getting it to print more than just the first character. I'm sure it's probably something stupid that I've overlooked. – Matt Coombs Sep 16 '17 at 23:31