2016-03-20 12 views
-2

2つのスタック(leftright)を持つテキストエディタバッファhwの割り当てがあります。すべては、ほとんどの場合、それが想定されている方法で動作します。しかし、私が抱えている問題は、それがあまりにも多くの空白を返すということです。私は具体的にテキストを返すためにtoString()メソッドを記入しようとしています。例えば リターンテキストを印刷:文字間、単一のスペースと、各単語の間に二重のスペースがありますあまりにも多くの空白を削除するtoStringメソッド

T h e r e i s g r a n d e u r i n t h i s v i e w o f l i f e , 

。どのように私の文字列を返すように、言葉だけの間で1つの空白を除去しながら、私は、文字間の空白を削除するには:

There is grandeur in this view of life, 

public class Buffer { 
    private Stack<Character> left; // chars left of cursor 
    private Stack<Character> right; // chars right of cursor 

    // Create an empty buffer. 
    public Buffer() { 
     left = new Stack<Character>(); 
     right = new Stack<Character>(); 
    } 

    // Insert c at the cursor position. 
    public void insert(char c) { 
     left.push(c); 
    } 

    // Delete and return the character at the cursor. 
    public char delete() { 
     if (!right.isEmpty()){ 
      return right.pop(); 
     }else return 0; 
    } 

    // Move the cursor k positions to the left. 
    public void left(int k) { 
     while (!left.isEmpty() && --k >= 0){ 
      right.push(left.pop()); 
     } 
    } 

    // Move the cursor k positions to the right. 
    public void right(int k) { 
     while (!right.isEmpty() && --k >=0){ 
      left.push(right.pop()); 
     } 
    } 

    // Return the number of characters in the buffer. 
    public int size() { 
     return left.size()+right.size(); 
    } 

    // Return a string representation of the buffer with a "|" character (not 
    // part of the buffer) at the cursor position. 
    public String toString() { 

     String a = (left+"|"+right); 


     return a; 
    } 

    // Test client (DO NOT EDIT). 
    public static void main(String[] args) { 
     Buffer buf = new Buffer(); 
     String s = "There is grandeur in this view of life, with its " 
      + "several powers, having been originally breathed into a few " 
      + "forms or into one; and that, whilst this planet has gone " 
      + "cycling on according to the fixed law of gravity, from so " 
      + "simple a beginning endless forms most beautiful and most " 
      + "wonderful have been, and are being, evolved. ~ " 
      + "Charles Darwin, The Origin of Species"; 
     for (int i = 0; i < s.length(); i++) { 
      buf.insert(s.charAt(i)); 
     } 
     buf.left(buf.size()); 
     buf.right(97); 
     s = "by the Creator "; 
     for (int i = 0; i < s.length(); i++) { 
      buf.insert(s.charAt(i)); 
     } 
     buf.right(228); 
     buf.delete(); 
     buf.insert('-'); 
     buf.insert('-'); 
     buf.left(342); 
     StdOut.println(buf); 
    } 
} 
+1

[最小限で完全であり、検証可能な例](http://stackoverflow.com/help/mcve)を投稿してください。 – MikeCAT

+0

あなたは、このサイトのルールに従って、あなたの質問をあなたの問題を解決するために最善の善意の試みを示すべきです。 [宿題に関する質問と回答方法](http://meta.stackexchange.com/a/10812/162852)もご覧ください。この情報は、質問が宿題や家事のためであるかどうかにかかわらず有効です(自習)。 –

+0

toString()メソッドのコードは何ですか? –

答えて

1

あなたのコードはStacktoString()方法に依存しています。フォーマットが明確に定義されていない限り、それを行うべきではありません。他のコレクションクラスと同じ方法(例:[value1, value2, value3])を出力しますが、java.util.Stackについては明確に定義されていません。人々があなたを語ってきたよう

[]|[s, e, i, c, e, p, S, , f, o, , n, i, g, i, r, O, , e, h, T, , ,, n, i, w, r, a, D, , s, e, l, r, a, h, C, , -, -, , ., d, e, v, l, o, v, e, , ,, g, n, i, e, b, , e, r, a, , d, n, a, , ,, n, e, e, b, , e, v, a, h, , l, u, f, r, e, d, n, o, w, , t, s, o, m, , d, n, a, , l, u, f, i, t, u, a, e, b, , t, s, o, m, , s, m, r, o, f, , s, s, e, l, d, n, e, , g, n, i, n, n, i, g, e, b, , a, , e, l, p, m, i, s, , o, s, , m, o, r, f, , ,, y, t, i, v, a, r, g, , f, o, , w, a, l, , d, e, x, i, f, , e, h, t, , o, t, , g, n, i, d, r, o, c, c, a, , n, o, , g, n, i, l, c, y, c, , e, n, o, g, , s, a, h, , t, e, n, a, l, p, , s, i, h, t, , t, s, l, i, h, w, , ,, t, a, h, t, , d, n, a, , ;, e, n, o, , o, t, n, i, , r, o, , s, m, r, o, f, , w, e, f, , a, , o, t, n, i, , r, o, t, a, e, r, C, , e, h, t, , y, b, , d, e, h, t, a, e, r, b, , y, l, l, a, n, i, g, i, r, o, , n, e, e, b, , g, n, i, v, a, h, , ,, s, r, e, w, o, p, , l, a, r, e, v, e, s, , s, t, i, , h, t, i, w, , ,, e, f, i, l, , f, o, , w, e, i, v, , s, i, h, t, , n, i, , r, u, e, d, n, a, r, g, , s, i, , e, r, e, h, T] 

文字列全体が、逆転され、ブラケット([])とコンマがあります

は(私がSystem.outStdOutを交換したら)出力の例についてはthis IDEONEを参照してください。スペース区切り文字(,)。

これが表示されない場合は、おそらくjava.util.Stackを使用していない可能性があります。

とにかく、解決策は、それが出力スペースがなかった場合でも、「カーソルが」の途中にある場合、あなたはまだ悪い結果を取得したいので、StacktoString()に依存しないためにあなたのtoString実装を修正することですあなたのテキスト。

あなたがjava.util.Stackを使用している場合は、その後、right.listIterator(right.size())を使用して文字を追加するhasPrevious()previous()を使用して逆方向反復rightを繰り返す、|を追加し、leftを反復処理し、各文字を追加し、その後、StringBuilder()を作成する必要があります。

+0

あなたは正しいです、それは標準のjava.util.stackを使用していません。スタックのプリンストンバージョン(違いがある場合)。出力は私のようになります http://imgur.com/A2lopuW 私は教授が私たちに求めたので、私はtoStringメソッドを記入する必要があります。 –

+0

* "toStringメソッドを記入する必要があります" *はい、これも私が言っていたことです。 *あなたの* 'toString()'メソッドを動作させるものに変更しなければなりません。つまり、 'left +" | "+"という式を暗黙的に実行しているので、 'Stack.toString()右。 – Andreas

+0

ああ、私はそれを得る。あなたがこれを達成する方法を教えてくれる方向はありますか?もう一度答えを探すのではなく、ここからどの方向へ進むべきかを指しています。 –

0

のいずれかがあなたのアルゴリズム(優先)を固定、またはここに簡単な「間に合わせ」方法です:

String fixed = str.replaceAll("(?<!) ", ""); 

マッチ正規表現は、「空白文字によって先行されていない空白文字」を意味します。

関連する問題