从堆栈打印时如何跳过空格

How do i skip blank spaces when printing from a stack

当我打印堆栈中的元素时,每个元素之间会有大量的空格或空格。

如果可能的话,我希望能够将它们打印得更靠近一些。

它会将堆栈中的元素保留在它们的位置,并且只否定除我当前抓取的那个以外的所有字母。

import java.io.*;

public class File 
{   
     public static void main(String[] args) {   
        try {
            BufferedReader in = new BufferedReader( new 
                                           FileReader("input.txt"));
            String str;
            while( (str = in.readLine()) != null){
                System.out.println("I just read: "+ str); 
                StackOfStrings[] ss = new StackOfStrings[35];
                for(int i=0; i <= 9; i++){
                    ss[i] = new StackOfStrings();    
                }
                if(str.charAt(4) == 'm') { 
                    ss[0].push("m");            
                }           
            }
            in.close(); 
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

这是没有界面的另一半

public class StackOfStrings implements StackInterface {

    private Node first; //top
    private int n;

    public StackOfStrings(){
        first = null;
        n=0;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return n;
    }

    public void push(String item)
    {
        Node x = new Node();
        x.item = item;
        x.next = first;
        first = x;
    }

    public String pop() {
        String val = first.item;
        first = first.next;
        n--;
        return val;
    }

    private class Node {
        String item;
        Node next;
    }

public String toString()
{
    String s="";
    Node t = first;
    while(t != null) {
        s += t.item + " ";
        t= t.next;
    }
    return s;
}
}

只需添加一个具有以下签名的新方法:

@Override
public String toString() {
    // Create some string based on your class
    return string;
}