0

I have to write a code that prints a triangle which the length of the last line is the input number. Also the empty spaces have to be filled with dots.

It has to look like this.

Also, the code has to run until the input is a uneven number.

Sadly, my code doesn't print out the dots or the other stars, but just the last line.

Can someone maybe look over it and give me some hints?

Any help appreciated. Thank you very much in advance.

Here is the code:



import java.util.Scanner;

public class Triangle_new {

    //Main-Method with Input-Check

public static void main(String[] args) {

    System.out.println("Type in a number to get the triangle:");


        Scanner sc = new Scanner(System.in);

        int length = sc.nextInt();



        if(length < 0 || length%2 == 0) {

                System.out.println("Please type in a uneven number or zero to quit. ");
                Triangle_Test.check_input(length);

        }else{
            display_triangle(length);
             }  

    }

    //DisplayTriangle-Method


    public static void display_triangle(int length) {

    int lines = (length+1)/2;

        //Height of the Triangle
    get_lines(length);

        //Dotfiller Left Side
    if(lines > 0){
    get_dotsleft(lines, length);
    }
        //Stars-Output
    if(lines > 0) {
    get_stars(lines);
    }
        //Dotfiller Right Side
    if(lines > 0) {
    get_dotsright(lines, length);
    }

    }

    //Constructors for Triangle

    public static void get_lines(int length) {

        for(int lines = 1; lines <= (length + 1) / 2; lines++){
        System.out.println();
        }
    }

    public static void get_dotsleft(int lines, int length){
        for(int leftfiller = 1; leftfiller <= ((length + 1) / 2) - lines; leftfiller++) {
        System.out.print(".");

        }
    }

    public static void get_stars(int lines) {
        for(int stars = 1; stars <= (2 * lines) - 1; stars++) {
        System.out.print("*");

        }
    }



    public static void get_dotsright(int lines, int length){
        for(int rightfiller = 1; rightfiller <= ((length+1) / 2) - lines; rightfiller++) {
        System.out.print(".");
        }
    }


}


    //Triangle_Test Class with Input-Check Method

class Triangle_Test extends Triangle_new {

    public static void check_input(int length) {


    //Check Input until Input is uneven number  

    Scanner sc = new Scanner(System.in);

    do {

    length = sc.nextInt();

    if(length%2 == 0 && length != 0) {
        System.out.println("Invalid input. Try again.");
                      }


    if(length%2 != 0 && length > 0) {

                    }

    //Triangle Output               

    if(length%2 != 0){

    Triangle_new.display_triangle(length);  

    }               

    if(length == 0) { 
    System.out.println(" -> Zero detected. Program will be shutdown.");
    }



    }while(length%2 != 0 || length != 0);

    }

}



Felix
  • 1
  • 1

3 Answers3

0

Your get_lines() method sounds like it should return how many lines the triangle will fill (also based on your comment), what it does, though, is print empty lines. Try not using it.

0

Change your display_triange function as follows:

public static void displayTriangle(int length) {
        String stars = "*";
        for (int i =0 ; i< ((length+1) /2); i++){
            String dots = "";
            for (int j = 0; j < (length - i * 2) / 2; j++) {
                    dots += ".";
                }
            System.out.println(dots+stars+dots);
            stars +="**";
        }
}

Let me explain why your solution didn't work:

  1. Your get_lines loops to just print empty lines.
  2. You should display all in one big loop (that represents number of lines), and inside this loop you calculate number of stars and dots to be displayed.
  3. Note that: Number of dots + number of stars in each line always equals the total number entered by the user.
  4. Number of dots to the left always equals number of dots to the right of stars and each side = (total number entered by user - number of stars)/2.
  5. Each line increments number of stars by 2.

This is how I thought about the problem and hence came my solution.

Marwa Eldawy
  • 191
  • 2
  • 5
0

Here are the problems in your code:

  1. get_lines is just printing some new lines in a for loop, while we simply need only one new line after each stars/dots combination. In other words, number of new lines doesn't change. it will always be one per line.

  2. get_dotsLeft and get_dotsRight are actually the same. They should be combined in only one function.

  3. the limits for the loops printing dots and stars were not correct. I fixed them.

  4. By convention, you better have to follow the camel case naming when you name your functions, i.e, get_lines should be getLines.

  5. all your get_ functions should be private. They should be only accessed in your class and shouldn't be exposed to other classes. (This won't throw errors, but it is not a good design to make those functions public).

    public static void display_triangle(int length) {
    int lines = (length+1)/2;
    for (int i =1 ; i<= lines ;i++) {
        //Height of the Triangle
        get_lines(length);
        get_dotsleft(lines-i);
        get_stars(i);
        get_dotsright(lines-i);    
      }
    }
    
    //Constructors for Triangle
    private static void get_lines(int length) {                     
      System.out.println(""); 
    }
    
    private static void get_dotsleft(int lines){
        for(int leftfiller = 1; leftfiller <= lines; leftfiller++) {
            System.out.print(".");
        }
    }
    
    private static void get_stars(int lines) {
        for(int stars = 1; stars <= (2 * lines) - 1; stars++) {
            System.out.print("*");
        }
    } 
    private static void get_dotsright(int lines){
        for(int rightfiller = 1; rightfiller <= lines; rightfiller++) {
            System.out.print(".");
        }
    }
    
Marwa Eldawy
  • 191
  • 2
  • 5
  • When trying to compile the code with the additions you suggested, I get the following error codes: I posted them here, as they surpass the number of characters allowed. https://justpaste.it/6oojm – Felix Nov 25 '19 at 18:38
  • @Felix They don't compile and give errors because I changed the number of parameters passed to get_dotsleft, get_stars and get_dotsright. It seems, somewhere in your code, you still have them taking two parameters instead of one. – Marwa Eldawy Nov 25 '19 at 19:20
  • Alright I got that fixed out. But now the first row of stars of the triangle does not show up. For example input 7 the triangle would have to have 4 rows of stars with 7 - 5 - 3 -1 stars. Now it only has 7 - 5 - 3. What am I supposed to change? – Felix Nov 25 '19 at 19:55
  • @Felix I edited the post. You just need to change the loop in display_triangle method so that it says : ```for (int i =1 ; i<= lines ;i++)```, I put ```i<=``` instead of ```i<``` – Marwa Eldawy Nov 26 '19 at 08:31