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’
Output:
X
X X
X X 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.

java
view plainprint?

1. package Programming1;
2. import java.io.BufferedReader;
3. import java.io.IOException;
4. import java.io.InputStreamReader;
5.
6.
7.
8. public class PrintNumberPattern
9. {
10. //-----------------------------------------------------------------
11. // Prints a triangle shape using asterisk (star) characters.
12. //-----------------------------------------------------------------
13.
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;
20.
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) {
26.
27. e.printStackTrace();
28. }
29.
30. BufferedReader charInput = new BufferedReader(new InputStreamReader(
31. System.in));
32.
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) {
38.
39. e.printStackTrace();
40. }
41.
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 ("*");
47.
48. System.out.println();
49. }
50. }
51. }

Recommended Answers

All 5 Replies

Change this:

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

To This:

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

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


This:

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

 System.out.println();
 }

Should be:

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

 System.out.println();
 }

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 "*"

what would i do for
BLUEJ
BLUEB
BLUBL
BLBLU
BBLUE

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){
no

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

 System.out.println();

 for(int row=0; row<numstar; row++)
 {
     for(int col=0; col<numstar-row; col++)
     {
        System.out.print("X");
        System.out.print(" ");
     }
    System.out.println();
 }
 }}

sokal: any particular reason you felt compelled to revive a dead thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.