2017-11-24 13 views
0

を空白ために空白を追加:Javaは、私は解決不可能に見える小さな問題持って

GOAL:正当取得するには、単一の空白文字に空白を追加することによって、ArrayListの中にテキストのために必要なだけの行(文字列)を揃えを。

package com.mycompany.app; 

import java.util.ArrayList; import java.util.List;

パブリッククラスMaxLengthLine {

String[] words; 
int size; 
int qtySpaces; 
public MaxLengthLine (String text, int size){ 
    this.words = text.split(" "); 
    this.size = size; 
} 
List<String> lines = new ArrayList<String>(); 
public void lineResize() { 


    int index = 0; 
    for (int i = 0; i < words.length - index; i++){ 
     String curLine = ""; 
    while((curLine + words[index]).length() <= size){ 
     curLine += words[index] + " "; 
     index++; 
    } 

    curLine = curLine.substring(0, curLine.length()-1); 
    lines.add(curLine); 
    } 

    String curLine = ""; 
    while(index < words.length){ 
     curLine += words[index] + " "; 
     index++; 
    } 

    curLine = curLine.substring(0, curLine.length()-1); 
    lines.add(curLine); 

} 

public void lineJustify() { 
    for (int i = 0; i < lines.size(); i++){ 
     while (lines.get(i).length() < size){ 
      String test = lines.get(i).replaceFirst(" ", " "); 

      lines.set(i, test); 
     } 
    } 
} 

public String getTextFull(){ 
    String output = ""; 
    for(int i = 0; i < lines.size();i++){ 
     output += lines.get(i) + "\n"; 
    } 
    while (output.contains(" ")){ 
     output = output.replace(" ", " "); 
    } 
    return output; 
} 

}

このコードは、私は(私はすでに他の多くを試みた以外に)最初に考え最もstraightfoward溶液であるが、何らかの理由で結果が来続けます同じ。

実際の出力:

In the beginning God created the heavens 
and the earth. Now the earth was 
formless and empty, darkness was over 
the surface of the deep, and the Spirit 
of God was hovering over the waters. 

And God said, "Let there be light," and 
there was light. God saw that the light 
was good, and he separated the light 
from the darkness. God called the light 
"day," and the darkness he called 
"night." And there was evening, and 
there was morning - the first day. 

所望の出力:

In the beginning God created the heavens 
and the earth. Now the earth was 
formless and empty, darkness was over 
the surface of the deep, and the Spirit 
of God was hovering over the waters. 

And God said, "Let there be light," and 
there was light. God saw that the light 
was good, and he separated the light 
from the darkness. God called the light 
"day," and the darkness he called 
"night." And there was evening, and 
there was morning - the first day. 

編集:入力:初めに

は、神が天と地を創造しました。今や地球は無秩序で空であり、暗闇は深い表面上にあり、神の霊は水の上に浮かんでいます。 神は「光があるように」と言って、光があった。神は光が良いことを見て、光を闇から分けました。神は光を「日」と呼び、暗闇を「夜」と呼びました。そして夜があり、朝があった - 最初の日。

(最後の部分は40文字までのテキストを正当化するために、その機能ですので、私はすでに、言葉を壊すことなく40文字で正確にラインを壊しコードを持っている)

EDIT 2:私は、コードのその部分を変更クラス全体より明確にするために、私の精巣に設定されているサイズは、JavaのStringクラスから40

+0

そして...例えば、入力? – alfasin

+3

何も正当化する必要はありません - あなたが到達しようとしている幅はどこですか? – John3136

+0

変数 'size'はどこで計算されますか? –

答えて

1
public static List<String> justifyLines(String input, int lineLength) { 
    String[] words = input.split(" "); 
    List<String> result = new ArrayList<>(); 
    StringBuilder line = new StringBuilder(); 
    //here we store positions of all spaces in the current line to add more spaces there 
    List<Integer> spacesPositions = new ArrayList<>(); 
    for (String word : words) { 
     if (word.length() <= lineLength - line.length()) { 
      line.append(word).append(" "); 
      spacesPositions.add(line.length() - 1); 
     } else { 
      result.add(justifyLine(line, lineLength, spacesPositions)); 
      line.setLength(0); 
      spacesPositions.clear(); 
      line.append(word).append(" "); 
      spacesPositions.add(line.length() - 1); 
     } 
    } 
    if (line.length() > 0) { 
     result.add(justifyLine(line, lineLength, spacesPositions)); 
    } 
    return result; 
} 

private static String justifyLine(StringBuilder line, int lineLength, List<Integer> spacesPositions) { 
    //if line ends with space - remove it 
    if (line.lastIndexOf(" ") == line.length() - 1) line.setLength(line.length() - 1); 
    int spacesToAdd = lineLength - line.length(); 
    for (int j = 0; j < spacesToAdd; j++) { 
     //It's the most complicated part, but I'll try to explain 
     line.insert(
       // We're adding one space to each space in the line and then, if there are still spaces to insert, 
       // repeating this process from the beginning - that's why we're using % 
       spacesPositions.get(j % (spacesPositions.size() - 1)) 
         // But each time we insert a new space, we need to take it into account for the following positions 
         // j % (spacesPositions.size() - 1) is the number of space in the line 
         // j/(spacesPositions.size() - 1) + 1 is the iteration number 
         + j % (spacesPositions.size() - 1) * (j/(spacesPositions.size() - 1) + 1), " "); 
    } 
    return line.toString(); 
} 

のでfor (String s : justifyLines("In the beginning...", 40)) System.out.println(s);プリント:

In the beginning God created the heavens 
and the earth. Now the earth was 
formless and empty, darkness was over 
the surface of the deep, and the Spirit 
of God was hovering over the waters. And 
God said, "Let there be light," and 
there was light. God saw that the light 
was good, and he separated the light 
from the darkness. God called the light 
"day," and the darkness he called 
"night." And there was evening, and 
there was morning - the first day. 
+1

ありがとうございます、非常に近いですが、このコードには2つの問題があります。問題1例: 行1:最初に神が を作成しましたが、この行に "heavens"コードは右に1つのスペースを追加して、それがどのようにすべきかを見ています。 最初に神が天を作成しました 問題2の例: 最後の行:最初の日。 最後の行と同じにする必要がありますので、次のように* something *を見てください。 最初の日。 おそらくこの問題は、私はちょうど挿入物をもう一度行うことができますが、最初は本当に見つけることができませんでした –

+0

あなたは余分なスペースについて正しいです。私はそれを修正しました。また、最後の行のアライメントも追加されました。編集された答えをチェックしてください! –

0

です:

パブリック文字列replaceFirstという(文字列の正規表現、 文字列置換)

この正規表現に一致するこの文字列の最初の部分文字列を、指定された置換文字に置き換えます。

したがって、スペース自体ではなく、正規表現を付ける必要があります。空白のJava正規表現でも「\ s」の

String test = lines.get(i).replaceFirst("\\s", " ");

で、考慮すべきものとして、replaceFirstというだけで正規表現に一致する最初の部分文字列を置き換えるので、このコードは最初の空白にあなたを空白を追加します(二重スペースの最初のスペースは正規表現 "\ s"に一致しますので)。

これをチェックしてください。

+0

答えに感謝しますが、スペースの識別は問題ではありません。問題は置換です。例えば、 "aa"のような文字の置換えを変更すると正しく置換されますが、何らかの理由で空白が追加されませんアップ。 1または10の空白を置くと、出力には1だけが残ります。すべてのイデアはなぜですか? –

関連する問題