Give default space to display Java text

0

It has been presented to me several times that I want to show several data by the console in an orderly manner and the different sizes of these elements make them look bad ... how do I do it so that in each part for the text is determined by a certain spacing?

Example: showing on screen

Name: / Code:        / /               / Abc / 123                / Eiu / 12       / Aaaaaa / 3333333

The idra is that there should be a space already established and that those hatches that box always have the same size

Example: Name: / Code:        / /        / Abc / 123        / Eiu / 12       / Aaaaaa / 3333333

Note: When I get to my pc I put an example with code.

    
asked by HeckDan 24.10.2017 в 19:52
source

1 answer

0

I put you as a job myself and you already use what works for you.

 /**
     * Formatea un texto  en base al espacio que se le asigne de manera siguiente:
     * <br>totalEspacio=10
     * <br>texto='Hola'
     * <br>caracter='|'
     * 
     * <br><br>Resultado: <p> "Hola_____|"</p> (El caracter '_' representa un
     * espacio en blanco.)
     * <br>
     * Si el texto supera a el espacio asignado entonces se acortara con '...' 
     * más el caracter de separación asignado
     * 
     * @param totalEspacio El espacio total del que se quitara el string de separación
     * y el tamaño del texto. El mínimo permitido es 5.
     * @param texto
     * @param caracterDeSeparación
     * @return
     */
    public static String formatearEspacios(int totalEspacio, String texto, String caracterDeSeparación){
        try {


            String espacio = "";
            String cadenaNueva = "";
            if (totalEspacio>texto.length()) {
                int espaciosEnBlanco = totalEspacio-texto.length();
                for (int i = 0; i < espaciosEnBlanco-1; i++) {
                    espacio+=" ";
                }
                espacio+=(caracterDeSeparación);
                cadenaNueva += texto+espacio;
            }else{
                String subTexto = texto.substring(0, totalEspacio-4);

                espacio+="..."+caracterDeSeparación;
                cadenaNueva += subTexto+espacio;

            }

            return cadenaNueva;

        } catch (ExcepcionPersonalizada ex) {
            Logger.getLogger(Textos.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

I implement it within a class Textos and I call it this way:

String a  = Textos.formatearEspacios(12, rvo.getCodigoProveedor(), "|");
String b  = Textos.formatearEspacios(25, rvo.getNombre(), "|");
String c  = Textos.formatearEspacios(25, rvo.getCodigoInterno(), " ");
System.out.println(a+b+c);
    
answered by 24.10.2017 в 20:00