0

私は小さなプロジェクトに取り組んでおり、ほとんどうまくいっています。全体的な目的は、ファイル ループを実行することであり、個々の行は何らかの方法で区切られます。コンマ、パイプなどについてはこれを行いましたが、固定幅バージョンも行う必要があります。以下のコードを書きました。これは、行上のすべての要素を検索して、文字列値を持つ要素のみを分離し、それを arrayList に追加する必要があります。

現在、最初の値を追加し、次に空白を表す一連のコンマを追加していますが、2 番目の文字列値には到達しません。

public static List fwListCreator(String str) throws IOException {

 List<String> fixedWidthList = new ArrayList<String>();
  char[] charArray = str.toCharArray();
    List<List<Boolean>> zeroWhite = new ArrayList<>();
    
    zeroWhite.add(new ArrayList<Boolean>()); 
    List<Boolean> temp = zeroWhite.get(0);

    //add all non-whitespace character from string
    for (char ch : charArray) {
         temp.add(!Character.isWhitespace(ch)); 
    }
    
    System.out.println(temp);

     //get all nonwhitespace characters per column

    int maxLine = zeroWhite.stream().mapToInt(e -> e.size()).max().orElse(0);
    System.out.println(maxLine);

     //max number of characters in row.
    int[] charCountArray = new int[maxLine];
    //counting all non-whitespace per column
    for (List<Boolean> row : zeroWhite) {
        for (int columnChars = 0; columnChars < row.size(); ++columnChars) {
            if (row.get(columnChars)) {
                ++charCountArray[columnChars];
            }
        }
    }
    //overview of non-white columns
    Map<Integer, Long> potentialMap = (Map<Integer, Long>) Arrays.stream(charCountArray).mapToObj(i -> (Integer)i).collect(Collectors.groupingBy( Function.identity(), // their identity (value)
                 Collectors.counting()));

    //minimum number of non-whitespace columns
    int emptyIntVal = Collections.min(potentialMap.keySet());
    //find delimited columns
    List<Boolean> emptyListVal= Arrays.stream(charCountArray).mapToObj(n -> n == emptyIntVal).collect(Collectors.toList());
    
    List<Integer> valIndices = new ArrayList<>();
    
    for (int charCount = 0; charCount < maxLine; ++charCount) {
        if (emptyListVal.get(charCount)) {
            valIndices.add(charCount);
        }
    }
    System.out.println(valIndices);
    
    int indexSizeVal = valIndices.size();
    valIndices.add(0,0);
  
     int len = str.length();
     //parse
     for (int i = 1; i <= indexSizeVal; ++i) {
         if (len < valIndices.get(i)) break;
                
         fixedWidthList.add(str.substring(valIndices.get(i-1), valIndices.get(i)).trim()); 

         }
return fixedWidthList

 }

これらはファイルに渡される 3 行で、毎回 1 行であるため、str は一度にこれらの行の 1 行を表します。

 Ackerman        Scott
 Jones           Steve
 Gaiman          Neil
4

1 に答える 1

0

これはあなたが探していたものですか?あなたのデータソースが何であるかわからないので、String[][]

public static String format(String[][] columns){
    int[] maxSize = new int[columns[0].length];
    for (int i = 0; i < maxSize.length; i++) {
        int max = 0;
        for (String[] column : columns) {
            max = Math.max(max, column[i].length());
        }
        maxSize[i] = max;
    }
    StringBuilder sb = new StringBuilder();
    for (String[] column : columns) {
        for (int i = 0; i < column.length; i++) {
            sb.append(String.format("%-" + maxSize[i] + "s", column[i]));
        }
        sb.append("\n");
    }
    return sb.toString();
}
于 2021-10-20T20:21:08.370 に答える