通过列表一次打印一个字符

时间:2017-09-16 18:01:19

标签: java list arraylist

我需要使用列表打印文本文件中的每个字母。我不确定它目前在哪里弄乱。

这是我到目前为止的代码。它目前只打印出文件中的第一个字母。例如,如果第一个字符是“H”,它将打印出“H”而不是继续文件的其余部分。我测试了多个文件,以确保它不仅仅是我正在使用的文件。它从标准的.txt文件中获取该短语。

任何帮助将不胜感激!

 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();
    }
    }

1 个答案:

答案 0 :(得分:0)

虽然没有经过测试,但问题可能是由if条件造成的,这种情况目前是多余的:

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

理想情况下应该是:

if (inputString.charAt(inputStringIndex) <= outputList.get(outputListIndex)) {
    displayCharArray(outputList);
    break;
}