在新行Java上打印Arraylist中的多个(不是全部)元素

时间:2014-11-14 04:02:09

标签: java arraylist println

我正在尝试一次打印一些结果并换行以防止在输出中传递80个字符列。我不想使用硬编码的行值,而是希望一次打印出几个元素直到完成。 Arraylist是地图中的一个值,所以对于每个单词我都希望输出看起来像这样

word = [12,456,345,134,

536,6346,3426,2346,

2346,2347,36787,46789]

而不是:

word = [12,456,345,134,536,6346 ......

涉及的课程:

package java112.analyzer;

import java.io.*;
import java.util.*;

/**
 *  This is the KeywordAnalyzer class. <br>
 *
 *@author mgundrum
 *
 */
public class KeywordAnalyzer implements Analyzer {

    private Map<String, List<Integer>> keywordMap = new TreeMap();
    private Properties properties;
    private int tokenOccurence = 1;

    /**
     *This is a constructor method for the TokenCountAnalyzer class
     */
    public KeywordAnalyzer() {

    }

    /**
     *This is a constructor method for the TokenCountAnalyzer class
     *
     *@param properties     The properties object passed from AnalyzeFile
     */
    public KeywordAnalyzer(Properties properties) {
        this.properties = properties;
        readFile();
    }

    public Map<String, List<Integer>> getKeywordMap() {
        return keywordMap;
    }

    public void readFile() {
        BufferedReader input = null;

        try {
            //create a BufferedReader to read the input file
            input = new BufferedReader(new FileReader(properties.getProperty("file.path.keywords")));
            String inputLine = "";

            //loop through the input file one line at a time and split on white
            while (input.ready()) {
                inputLine = input.readLine();
                keywordMap.put(inputLine, new ArrayList<Integer>() );
            }
        } catch (java.io.FileNotFoundException fnfe) {
            System.out.println("Failed to read input file");
            fnfe.printStackTrace();
        } catch (Exception exception) {
            System.out.println("General Error");
            exception.printStackTrace();
        } finally {
            //Don't forget to close!
            try {
                if (input != null) {
                    input.close();
                }
            } catch (java.io.IOException ioe) {
                System.out.println("Failed to close input file");
                ioe.printStackTrace();
            }
        }
    }

    /**
     *  This method counts the total tokens as they are passed in
     *
     *@param  token - tokens passed from AnalyzeFile
     */
    public void processToken(String token) {       
       if (keywordMap.containsKey(token)) {
           List list = keywordMap.get(token);
           list.add( new Integer(tokenOccurence));
           keywordMap.put(token, list);
       }
       tokenOccurence++;
    }
    /**
     *  This method defines the TokenCount output file.
     *
     *@param inputFilePath - original file
     */
    public void writeOutputFile(String inputFilePath) {
        PrintWriter output = null;

        try {
            output = new PrintWriter( new BufferedWriter( new FileWriter(
                     properties.getProperty("output.dir") + 
                     properties.getProperty("output.file.keywords"))));

            for (Map.Entry<String, List<Integer>> entry : keywordMap.entrySet()) {
               output.println(entry.getKey() + " = ");
               output.println(entry.getValue());
               output.println();
            }
        } catch (IOException ioException) {
            System.out.println("FileWriter caused an error");
            ioException.printStackTrace();
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }
}

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

我编写了这个静态方法,它接受一个字符串并将其包装回指定的字符长度。我测试了一些基本的边缘情况,但您可能希望在将其用于任何事情之前对其进行更彻底的测试!

本质上,它从字符长度开始,然后返回寻找空格。如果找到一个,则用换行符替换它。如果它找不到,它期待着它们。如果它也失败了,它会放弃并返回它所拥有的一切。

这是:

public static String wordWrap(String rawInput, int maxLineLength) {
        StringBuilder input = new StringBuilder(rawInput);
        StringBuilder output = new StringBuilder();

        while(input.length() > maxLineLength) {
            for(int i = maxLineLength; i >= 0; i--) {
                //you can change this delimiter to whatever (or add it as an argument =D)
                if(input.charAt(i) == ' ') {
                    output.append(input.substring(0, i));
                    output.append('\n');
                    input.delete(0, i+1);
                    break;
                }

                if(i == 0) {
                    for(int j = maxLineLength; j < input.length(); j++) {
                        if(input.charAt(j) == ' ') {
                            output.append(input.substring(0, j));
                            output.append('\n');
                            input.delete(0, j+1);
                            break;
                        }

                        if(j == input.length() - 1) {
                            return output.append(input).toString();
                        }
                    }
                }
            }
        }

        return output.toString();
    }