Hi just wondering if anyone can advise why this is not working and offer a solution, I am new to java. Thanks in advance for any help.

Print Pattern Feature
The “print pattern” feature should function as follows:
The user should be prompted to enter the following input values:
! the number of rows in the pattern to be printed
! and the actual character to be printed in the pattern

The feature should then proceed to print a triangular pattern of the specified character
based on the number of rows specified by the user as follows:

number of rows: 3
character to be printed: ‘X’

Note that there is a space after each character that was printed.

The pattern should be printed using a nested loop which prints the characters in the
pattern one at a time (in the inner loop) – marks will be deducted from this section for
hard-coding pattern output in any way.

view plainprint?

1. package Programming1;
2. import java.io.BufferedReader;
3. import java.io.IOException;
4. import java.io.InputStreamReader;
8. public class PrintNumberPattern
9. {
10. //-----------------------------------------------------------------
11. // Prints a triangle shape using asterisk (star) characters.
12. //-----------------------------------------------------------------
14. public static void main (String[] args)
15. {
16. BufferedReader stdin = new BufferedReader(new InputStreamReader(
17. System.in));
18. int row = 0;
19. int numberOfLines = 0;
21. // Get integer input
22. System.out.println( " Please enter number of rows in integer type ");
23. try {
24. String MAX_ROWS = stdin.readLine();
25. } catch (IOException e) {
27. e.printStackTrace();
28. }
30. BufferedReader charInput = new BufferedReader(new InputStreamReader(
31. System.in));
33. // Get char input
34. System.out.println(" Please enter a character to be printed ");
35. try {
36. char MAX_ROWS = charInput.readLine().charAt(0);
37. } catch (IOException e) {
39. e.printStackTrace();
40. }
42. int MAX_ROWS = Integer.parseInt(args[0]);
43. for (int i = 1; i <= MAX_ROWS; i++)
44. {
45. for (int star = 1; star <= row; star++)
46. System.out.print ("*");
48. System.out.println();
49. }
50. }
51. }

Change this:

System.out.println( " Please enter number of rows in integer type ");
try {
String MAX_ROWS = stdin.readLine();
} catch (IOException e) {

To This:

String MAX_ROWS = "";
System.out.println( " Please enter number of rows in integer type ");
try {
    MAX_ROWS = stdin.readLine();
} catch (IOException e) {

In that way you will have access to the variable outside of the try.

Why do you need this when you already doing the above:
int MAX_ROWS = Integer.parseInt(args[0])

You read the MAX_ROWS from keyboard and then again from the arguments ????
Use the either the String MAX_ROWS and turn into an int or don't read from the keyboard and pass everything as arguments.
And the try-catch should be around your whole program. If something fails, you don't want to continue with the calculations


for (int i = 1; i <= MAX_ROWS; i++)
 for (int star = 1; star <= row; star++)
 System.out.print ("*");


Should be:

for (int i = 1; i <= MAX_ROWS; i++)
 for (int star = 1; star <= i; star++)
 System.out.print ("*");


Row is not declared and you want the 1st line to have 1 character, the 2nd line 2. That is why the upper limit of the second for-loop is the line you are now prinitng.

And WHY are you using the same variable for all the values. You have used the MAX_ROWS for getting the number of rows as String from console, from the arguments and for the char to be printed. Use different variables names and you must print what you get from the user, not the "*"

write the code, for starters.
also, don't hijack nor revive ancient threads, especially not to ask unrelated questions.

public static void main(String[] args){

 Scanner scan = new Scanner(System.in);
 System.out.print("Enter how many stars do you want to see at max : ");


 for(int row=0; row<numstar; row++)
     for(int col=0; col<numstar-row; col++)
        System.out.print(" ");
This article has been dead for over six months. Start a new discussion instead.