2

I am very new to coding and was just introduced to static methods, so I apologize in advance for the silly mistakes. The method should display a triangle when the method is called under main, but I am getting an empty console and there is no output. However, if I write this under main:

    String triangle = getTriangle(3, 4);    
    System.out.println(triangle);               

then, the triangle will be displayed in the console, but for this assignment, the string/triangle must be called by only using getTriangle(maxRows, maxCols)

public class Triangle {

    public static String getTriangle(int maxRows, int maxCols) {
        String T = "";

        if (maxRows < 1 || maxCols < 1) {
            return null;
        } else {
            for (int row = 1; row <= maxRows; row++) {
                for (int col = 1; col <= row; col++) {
                    T += "*";
                }
                T += "\n";  }   
            }
            return T;
        }
    }


    public static void main(String[] args) {
        getTriangle(3,2);           

    }

}
Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • Just stick `System.out.println(T);` at the very end of the `getTriangle` method and change it to return `void`, not `String`. Also, please pay attention to proper code formatting. It will help both you and others. – Fureeish Sep 22 '19 at 17:00
  • 1
    *returning* value != *printing/displaying* value. [Differences between System.out.println() and return in Java](https://stackoverflow.com/q/25456472) – Pshemo Sep 22 '19 at 17:00
  • `getTriangle` should return something (because of the word `get`). `printTriangle` should print it. Really, OP should probably just override `toString` – 001 Sep 22 '19 at 17:03

5 Answers5

3

You still need to print the result of getTriangle in your main method. Now you are just ignoring that result.

System.out.println(getTriangle(3,2));
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
1

Make your method void and print the T at the end of the method.

public static void getTriangle(int maxRows, int maxCols) {    
    if (maxRows < 1 || maxCols < 1) {
        return; 
    }

    String T = "";

    for (int row = 1; row <= maxRows; row++) {
        for (int col = 1; col <= row; col++) {
            T += "*";
        }
        T += "\n";
    }

    System.out.println(T);
}

Since you no longer return a triangle, you can rename the method to printTriangle. Personally, I would throw an exception if the condition maxRows < 1 || maxCols < 1 holds true, but it's a different topic.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

First of all, you should try to make your code more readable by indenting it properly.
Isn't it a lot easier to read like this?

public class Triangle {

    public static String getTriangle(int maxRows, int maxCols) 
    {
        String T = "";

        if (maxRows < 1 || maxCols < 1) 
        {
            return null;

        } 
        else 
        {
            for (int row = 1; row <= maxRows; row++) 
            {
                for (int col = 1; col <= row; col++) 
                {
                    T += "*";
                }
                T += "\n";  
            }   
        }
        return T;
    }

    public static void main(String[] args) 
    {
        getTriangle(3,2);           
    }
}

And secondly and more important, as stated by others, your main method should go

public static void main(String[] args) 
{
    System.out.println(getTriangle(3,2));
}

You are receiving the string, you are just not outputting it

newbie
  • 1,199
  • 1
  • 10
  • 25
0

you can add line System.out.println(T); before return T; to do so.

0

You can print it right before your method returns it.

public static String getTriangle(int maxRows, int maxCols) {
    String T = "";

    if (maxRows < 1 || maxCols < 1) {
        return null;

    } else {

        for (int row = 1; row <= maxRows; row++) {
            for (int col = 1; col <= row; col++) {
                T += "*";
            }
            T += "\n";
        }
    }
    System.out.println(T); // Print the triangle
    return T;
}

(A tiny personal opinion)

After that, the method will return and print the triangle hence I would not name the method getTriangle. Maybe something like getAndPrintTriangle...

George Z.
  • 6,643
  • 4
  • 27
  • 47