K
K
KianGreenMoon2018-10-04 20:01:56
Java
KianGreenMoon, 2018-10-04 20:01:56

How can I organize a data output that imitates a table without being one (I can't figure out how to organize a line break)?

I've been here for two days now. Here is the code:

int j = settings.getHeightPage();
        for(String[] person : sourceData.getPersonInfo())
        {
            //Названия колонн
            if(j == settings.getHeightPage())
            {
                System.out.println(head);
                j--;
            }

            //Разделитель
            System.out.println(new String(new char[settings.getWidthPage()]).replace("\0", "-"));

            //Output
            for(int i = 0; i < person.length; i++)
            {
                if(person[i].length() > settings.getWidth(i))
                {
                    person[i] = "Error" + new String(new char[settings.getWidth(i) - "Error".length()]).replace("\0", " ");
                }
                else
                    person[i] = person[i] + new String(new char[settings.getWidth(i) - person[i].length()]).replace("\0", " ");
            }
            System.out.println("| " + person[0] + " | " + person[1] + " | " + person[2] + " |");
            j--;

            //Разделение страниц
            if(j == 0 || person == sourceData.getPersonInfo().get(sourceData.getPersonInfo().size() - 1))
            {
                System.out.println("~");
                j = settings.getHeightPage();
            }
        }

Everything is fine except if(person[i].length() > settings.getWidth(i)). I don't need to write Error, but I need to break a long line into shorter lines and enter it in the same egg. The output looks like this:
| Номер    | Дата    | ФИО     |
--------------------------------
| 1        | 25/11       | Error     |
--------------------------------
| 2        | 26/11       | Error     |
--------------------------------
| 3        | 27/11       | Н/Д      |
--------------------------------
| 4        | 28/11   | Error        |
--------------------------------
| 5        | Error   | Error   |
~

And should be like this:
| Номер    | Дата    | ФИО     |
--------------------------------
| 1        | 25/11   | Иван         |
|           |              | Иванович |

Everything is aggravated by the fact that I will need to output to a file under a certain encoding, so I (maybe in vain, explain if so) eliminated the option to use a library for tables.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Karpion, 2018-10-04
@KianGreenMoon

1) It's not clear to me where the width of each column comes from. Is it known and hardcoded before the table is printed? Or is it calculated according to the contents of the table (as in HTML), and then it can be found out only after viewing the entire report (i.e. the report is printed in two passes)?
2) Is it possible that the first name / patronymic / last name will not fit into the column? Those. is it possible that the column "FULL NAME" will be three characters? What to do then?
3) The solution is probably in text browsers - lynx, links and the like.
Well, in Perl, among the modules, there are probably libraries for generating reports with your requirements.
4) The printing of each line of the report is the printing of several lines of text (in the table "A must be like this:" - two lines of text on the first line of the report).
Let's say the answer to the first question is "we know the width of the columns". Then the algorithm is something like this:
* Create an array of strings: "1", "25/11", "Ivan Ivanovich".
* Here we have the label loop - to organize the cycle. Of course, we will actually make the cycle on normal operators, as Niklaus Wirth taught.
* Iterate through the array, trying to print the contents. We managed to print the first two lines completely, they fit - so we reset them.
* But the third line did not fit. We print what fit - i.e. "Ivan". What is printed - we throw it out of the line (or, if in C - we play with pointers so as not to copy the contents of the lines).
* Yes, by the way, above we do not reset the entered lines, but in the same way we throw out what we printed. But since
* Along the way, we write to an integer variable - whether there are non-empty lines left.
* After the first pass, we have: "", "", "Ivanovich" (i.e. in this case - yes, non-empty lines remained). Repeat the operation (goto loop).
I think you should. understandably.
Upd: I can't think of a transfer criterion for you. But in your question it is not.

K
KianGreenMoon, 2018-10-04
@KianGreenMoon

int j = settings.getHeightPage();
        for(String[] person : sourceData.getPersonInfo())
        {
            //Названия колонн
            if(j == settings.getHeightPage())
            {
                System.out.println(head);
                j--;
            }

            //Разделитель
            System.out.println(new String(new char[settings.getWidthPage()]).replace("\0", "-"));

            //Вывод
            for(int i = 0; i < person.length; i++)
            {
                splitedText[i] = splitString(person[i],settings.getWidth(i));
            }
            while(splitedText[1].size() != splitedText[2].size())
            {
                if(splitedText[1].size() > splitedText[2].size())
                {
                    splitedText[2].add("");
                }
                else{
                    splitedText[1].add("");
                }
            }
            while(splitedText[0].size() != splitedText[1].size())
            {
                if(splitedText[0].size() > splitedText[1].size())
                {
                    splitedText[1].add("");
                }
                else{
                    splitedText[0].add("");
                }
            }
            for(int i = 0; i < splitedText[1].size(); i++)
            {
                splitedText[0].set(i, splitedText[0].get(i) + new String(new char[settings.getWidth(0) - splitedText[0].get(i).length()]).replace("\0", " "));
                splitedText[1].set(i, splitedText[1].get(i) + new String(new char[settings.getWidth(1) - splitedText[1].get(i).length()]).replace("\0", " "));
                splitedText[2].set(i, splitedText[2].get(i) + new String(new char[settings.getWidth(2) - splitedText[2].get(i).length()]).replace("\0", " "));
                System.out.println("| " + splitedText[0].get(i) + " | " + splitedText[1].get(i) + " | " + splitedText[2].get(i) + " |");
                j--;
            }

            //Разделение страниц
            if(j <= 0 || person == sourceData.getPersonInfo().get(sourceData.getPersonInfo().size() - 1))
            {
                System.out.println("~");
                j = settings.getHeightPage();
            }
        }

I'm ashamed to show it. A crutch on a crutch, and it doesn't work as it should, although it works :D Damn, I'm tired, I'll go eat :3 If someone still can tell me something, I'll be grateful. Good day

E
Evhen, 2018-10-05
@EugeneP2

For formatted output to the console, for example in the form of a table, there is a ready-made System.out method. printf(...) . Spend a little time studying the documentation and don't reinvent the wheel + google gives a bunch of examples, for example
https://c4code.wordpress.com/2014/03/17/how-to-pri...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question