2016-03-20 40 views
-2

我有一个文本编辑器缓冲区hw分配与2堆栈(leftright)。一切都按照它应有的方式运作。但是我遇到的麻烦是它返回的空白太多。我特意试图填写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()方法。除非格式明确,否则不应该这样做,并且java.util.Stack的格式没有明确定义,尽管它输出的方式与其他集合类相同,即[value1, value2, value3]

this IDEONE用于输出的一个例子(一旦我换成StdOutSystem.out):

[]|[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] 

整个字符串被逆转,因为人们一直在告诉你,有括号([])和逗号 - 空格分隔符(,)。

如果你没有看到,那么你可能不会使用java.util.Stack,但一些自行开发的实现。

无论如何,解决方法是修复您的toString实现不依靠StacktoString(),因为即使它没有输出空间,你还是会得到不好的结果,如果“光标”是在中间你的文字。

如果您正在使用java.util.Stack,您应该创建一个StringBuilder(),然后遍历left和追加每个角色,追加|,然后right使用right.listIterator(right.size())和使用hasPrevious()previous()追加字符迭代向后遍历。

+0

你是正确的,它不使用标准的java.util.stack。但普林斯顿版本的堆栈(如果有差异)。我得到的输出看起来像 http://imgur.com/A2lopuW 我必须填写toString方法,因为它的教授要求我们。 –

+0

*“我必须填写toString方法”*是的,这也正是我所说的。您必须将* your *'toString()'方法更改为有效的方法,这意味着不要使用'Stack.toString()'方法,因为您现在隐式地使用表达式'left +“|”+ right'。 – Andreas

+0

啊,好吧,我明白了。有没有一个方向可以指导我如何实现这一目标?再次,不寻找答案,而是从这里走向哪条路。 –

0

无论哪种解决您的算法(首选),或在这里是一个简单的“快速和肮脏”的方式:

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

匹配正则表达式的意思是“不是由一个空格字符前面的空格字符”。