Fotic
Fotic

Reputation: 321

Format Searched Output from TextArea

i need your help!

So basically i made a Search Student Tab who read a .txt file and output on textArea.

Image

But after search i take this:

Output result

But i want to take this:

as Output on TextArea

So here is my code:

 StringBuilder rsl = new StringBuilder();

    @FXML
    public void PressedSearch(ActionEvent event) throws IOException {                   //Ektelei Search sto arxeio ton mathiton
        if (searchStudent() == false) {
            result.setStyle("-fx-font:13 sherif;-fx-text-fill: red");
            result.setText("Failed to find the student");
        } else {
            result.setStyle("-fx-text-fill: black");
            rsl.setLength(0);
        }
    }

    public boolean searchStudent() throws IOException {         //Sto search o admin mporei na dei perissotera pragmata
        Path p = Paths.get("src", "inware", "users.txt");
        Scanner in = null;
        boolean i = false;
        in = new Scanner(p);
            while (in.hasNext()) {
                String line = in.nextLine();
                String[] fields = line.split("[,]");
                if (line.contains(search_field.getText())) {
                    rsl.append(fields[4] + "   " + fields[5]+ "   " + fields[6] + "   " + fields[7] + "   " + fields[8]
                            + "   " + fields[9] + "   " + fields[10]+ "   " + fields[11]+ "   " + fields[12]+ "   " + fields[13]
                            + "   " + fields[14]+ "   " + fields[15] + "   " + fields[16] + "   " + fields[17] + "   " + fields[18]
                            + "   " + fields[19] + "   " + fields[20] + "   " + fields[21]);
                    rsl.append("\n");  //Gia na kanei print polla atoma
                    i = true;
                }
            }
        result.setText(rsl.toString());
        in.close();
        return i;

Upvotes: 0

Views: 51

Answers (1)

Ioana Cutean
Ioana Cutean

Reputation: 21

Try to use String.format(String format,Object... args).

Instead of using:

rsl.append(fields[4] + "   " + fields[5]+ "   " + fields[6] + "   " + fields[7] + "   " + fields[8]
                        + "   " + fields[9] + "   " + fields[10]+ "   " + fields[11]+ "   " + fields[12]+ "   " + fields[13]
                        + "   " + fields[14]+ "   " + fields[15] + "   " + fields[16] + "   " + fields[17] + "   " + fields[18]
                        + "   " + fields[19] + "   " + fields[20] + "   " + fields[21]);
                rsl.append("\n");  //Gia na kanei print polla atoma

You can use:

String format = "%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n";
rsl.append(String.format(format,fields[4],fields[5],fields[6],fields[7],fields[8],fields[9],fields[10],fields[11],fields[12],fields[13],fields[14],fields[15],fields[16],fields[17],fields[18],fields[19],fields[20],fields[21]));

In format String, the number between '%' and 's'( Width field) represents the minimum number of characters to output. You can change it according to your needs.

Upvotes: 2

Related Questions