Search code examples
javafxtriangle

Inverted half pyramid using numbers in JavaFX


I made a normal half pyramid in JavaFX with this code

public void buttonpressed() {
    int iNum = Integer.parseInt(Num.getText());
    String sResult = "";
    String sNumber = "";
    for (int i = 1; i<=iNum; i++ ) {
        sNumber += i;
        sResult += sNumber +  "\n";
        result.setText(sResult);
    }    
    }

Which outputs (if the inputted number was 5)

1
12
123
1234
12345

Now I have to make 2 triangles (one normal and one inverted) which would look like this (inputted number = 5)

1 12345
12 1234
123 123
1234 12
12345 1

The problem is that I don't understand how to make an inverted triangle, I found how to make one in Java

public class Main {

  public static void main(String[] args) {
    int rows = 5;

    for (int i = rows; i >= 1; --i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}

But I can't adapt it to JavaFX so please help me with that.

I tried copying the for loops and adapting it to JavaFX but it didn't work.

public void buttonpressed() {
      int iNum = Integer.parseInt(Num.getText());
      String sResult = "";
      String sNumber = "";
  for (int i = inum3; i >= 1; i--) {
             for (int j = 1; j <= i; ++j) {     
            sNumber+=j; 
            sResult += sNumber +"\n";
       
              }
          result.setText(sResult);
           }      
}

The result I want is

12345
 1234
  123
   12
    1

Instead, with the provided code, I get

1
12
123
1234
12345
123451
1234512
12345123
123451234
123451234123
1234512341231
12345123412312
123451234123121

The whole code is

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class invertedtriangle extends Application{
TextField num = new TextField();
Label result = new Label();    
public void start(Stage stage) {
    GridPane p = new GridPane();
    Scene scene = new Scene(p, 1000, 900);
    Label input = new Label("Input a number");
    Button press = new Button("Press");
    p.add(input,0,1);
    p.add(num, 0,2);
    p.add(press, 0, 3);
    p.add(result, 0, 4);
    press.setOnAction(e -> buttonpressed());
   p.setHgap(10.0);
    p.setVgap(7.0);
    stage.setTitle("inv triangle");
    stage.setScene(scene);
    stage.show();
}
public void buttonpressed() {
    int iNum = Integer.parseInt(num.getText());
    String sResult = "";
    String sNumber = "";
for (int i = iNum; i >= 1; i--) {
           for (int j = 1; j <= i; ++j) {     
          sNumber+=j; 
          sResult += sNumber +"\n";
     
            }
        result.setText(sResult);
         }      
}
        
public static void main(String[] args){
        launch (args);
    }    
}

Solution

  • Just use the solution you already have, appending text to a string instead of sending it to the console:

    public void buttonPressed() {
        int iNum = Integer.parseInt(num.getText());
        String sResult = "";
        for (int i = iNum; i >= 1; --i) {
            for (int j = 1; j <= i; ++j) {
                // System.out.print(j + " ");
                sResult = sResult + (j+" ");
            }
            // System.out.println();
            sResult = sResult + "\n";
        }
        result.setText(sResult);         
    }
    

    Note that it is far more efficient to use a StringBuilder instead of building up strings by concatenation (basically, in Java, you should never repeatedly perform string concatenation on the same string in a loop):

    public void buttonPressed() {
        int iNum = Integer.parseInt(num.getText());
        StringBuilder text = new StringBuilder();
        for (int i = iNum; i >= 1; --i) {
            for (int j = 1; j <= i; ++j) {
                text.append(j).append(" ");
            }
            text.append("\n");
        }
        result.setText(text.toString());         
    }