Stars pattern program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinNeedsJavaHelp
    New Member
    • Feb 2007
    • 22

    Stars pattern program

    I need the program to prompt the user to input an odd number between 3 and 79 and then the output will look like this:

    for example if they input 5:
    *
    **
    ***
    ****
    *****

    or 9:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
  • colinNeedsJavaHelp
    New Member
    • Feb 2007
    • 22

    #2
    here is my first attempt:
    Code:
    import java.io.*;
    import java.util.*;
    
    publc class MagicPrinter
    {
    publc static void main(String[] args);
    {
    String inputString;
    double num1;
    char a = "*";
    
    //for the input stream
    InputStreamReader isr = new InputStreamReader(System.in);
    //needed to use ReadLine()
    BufferedReader br = new BufferedReader(isr);
     
    System.out.println("How many lines? ";
    inputString = br.readLine();
    
    do{
    for(num 1 == 1;num1>3; a++){
      for(num1 ==1;num3<80;a++){
      System.out.print(a++);
     }
       }
        }

    Comment

    • abctech
      New Member
      • Dec 2006
      • 157

      #3
      Originally posted by colinNeedsJavaH elp
      here is my first attempt:
      Code:
      import java.io.*;
      import java.util.*;
      
      publc class MagicPrinter
      {
      publc static void main(String[] args);
      {
      String inputString;
      double num1;
      char a = "*";
      
      //for the input stream
      InputStreamReader isr = new InputStreamReader(System.in);
      //needed to use ReadLine()
      BufferedReader br = new BufferedReader(isr);
       
      System.out.println("How many lines? ";
      inputString = br.readLine();
      
      do{
      for(num 1 == 1;num1>3; a++){
        for(num1 ==1;num3<80;a++){
        System.out.print(a++);
       }
         }
          }
      Hello,
      First of all taking one look at your program I can say you must be getting more than a few compile time errors!

      -Giving a ';' after main()? That will make it abstract and its body will be ignored.

      -A character literal needs to be enclosed in single quotes(' ') not double(" ").

      -The method readLine() is capable of throwing an 'IOException'.T his exception is a type of 'Java Checked Exception', such type of exceptions must be handled in a try-catch block or declared to be thrown.

      - for(num1 == 1) is incorrect.
      '==' is for comparision and '=' is for assignment.

      Structure of a 'for' loop is :
      for (initialization ; termination; increment) {
      statement(s)
      }
      The first expression of the for loop is for initialization, but instead if you carry out comparision you'll definitely get an error.

      -num3<80 ?
      What is num3?Where is it declared?

      - There is no do-loop in Java, the loops that we have are for,while,do-while.

      Please compile your program and take a good look at the exceptions you are getting, try to sort them out one by one first. If you don't understand any exception post it.

      As for the logic:
      say there are 3 lines then -
      line 1: one *
      line 2: two *'s
      line 3: three *'s

      See the connection between the line number and the corresponding number of stars in it.
      Now generalize it.

      Comment

      • colinNeedsJavaHelp
        New Member
        • Feb 2007
        • 22

        #4
        Originally posted by abctech
        Hello,
        First of all taking one look at your program I can say you must be getting more than a few compile time errors!

        -Giving a ';' after main()? That will make it abstract and its body will be ignored.

        -A character literal needs to be enclosed in single quotes(' ') not double(" ").

        -The method readLine() is capable of throwing an 'IOException'.T his exception is a type of 'Java Checked Exception', such type of exceptions must be handled in a try-catch block or declared to be thrown.

        - for(num1 == 1) is incorrect.
        '==' is for comparision and '=' is for assignment.

        Structure of a 'for' loop is :
        for (initialization ; termination; increment) {
        statement(s)
        }
        The first expression of the for loop is for initialization, but instead if you carry out comparision you'll definitely get an error.

        -num3<80 ?
        What is num3?Where is it declared?

        - There is no do-loop in Java, the loops that we have are for,while,do-while.

        Please compile your program and take a good look at the exceptions you are getting, try to sort them out one by one first. If you don't understand any exception post it.

        As for the logic:
        say there are 3 lines then -
        line 1: one *
        line 2: two *'s
        line 3: three *'s

        See the connection between the line number and the corresponding number of stars in it.
        Now generalize it.
        Yeah I realized after posting I made a ton of errors. Here is what I have now
        Code:
        import java.io.*;
        import java.util.*;
        
        public class MagicPrinter
        {
        public static void main(String[] args)
        {
        String inputString;
        String = num1;
        char a;
        a = '*';
        
        //for the input stream
        InputStreamReader isr = new InputStreamReader(System.in);
        //needed to use ReadLine()
        BufferedReader br = new BufferedReader(isr);
         
        System.out.println("How many lines? ");
        num1 = br.readLine();
        
        //double num1 = Double.parseDouble(in.readLine());
        num1 = 0;
        do{
        System.out.println(a);
        a++;
        }while(num1<80);
        }
        }

        Comment

        • abctech
          New Member
          • Dec 2006
          • 157

          #5
          Originally posted by colinNeedsJavaH elp
          Yeah I realized after posting I made a ton of errors. Here is what I have now
          Code:
          import java.io.*;
          import java.util.*;
          
          public class MagicPrinter
          {
          public static void main(String[] args)
          {
          String inputString;
          String = num1;
          char a;
          a = '*';
          
          //for the input stream
          InputStreamReader isr = new InputStreamReader(System.in);
          //needed to use ReadLine()
          BufferedReader br = new BufferedReader(isr);
           
          System.out.println("How many lines? ");
          num1 = br.readLine();
          
          //double num1 = Double.parseDouble(in.readLine());
          num1 = 0;
          do{
          System.out.println(a);
          a++;
          }while(num1<80);
          }
          }
          Colin,
          Tell me how do we declare string literals in Java? And then check if you are doing it the right way.

          Your variable 'num1' is a String or an int? You want the user to input the number of lines dont you? 1,2,3 etc are numbers(integer s) not strings.

          You can simply say:
          Code:
          DataInputStream input = new DataInputStream(System.in);
          		
          System.out.println("How many lines you have to print?");
          int [B]num1 [/B] = Integer.parseInt(input.readLine());
          I also din't quite understand the logic behing your while loop, you want to print lines uptill num1(i.e the number of lines entered by user) or uptill 80?

          Here's a link to understand the various loops properly.

          And here are links to some previous problems similar to yours:




          Have a look and then try again.

          Comment

          • colinNeedsJavaHelp
            New Member
            • Feb 2007
            • 22

            #6
            Originally posted by abctech
            Colin,
            Tell me how do we declare string literals in Java? And then check if you are doing it the right way.

            Your variable 'num1' is a String or an int? You want the user to input the number of lines dont you? 1,2,3 etc are numbers(integer s) not strings.

            You can simply say:
            Code:
            DataInputStream input = new DataInputStream(System.in);
            		
            System.out.println("How many lines you have to print?");
            int [B]num1 [/B] = Integer.parseInt(input.readLine());
            I also din't quite understand the logic behing your while loop, you want to print lines uptill num1(i.e the number of lines entered by user) or uptill 80?

            Here's a link to understand the various loops properly.

            And here are links to some previous problems similar to yours:




            Have a look and then try again.
            I want to print lines between odd numbers 3 and 79

            Comment

            • colinNeedsJavaHelp
              New Member
              • Feb 2007
              • 22

              #7
              Ok so I assigned num1 to be an integer. Then I used this after the input string:

              Code:
              num1 = Integer.parseInt(input.readLine());
              and I get this error:

              Code:
              MagicPrinter.java:24: cannot find symbol
              symbol  : variable input
              location: class MagicPrinter
              num1 = Integer.parseInt(input.readLine());
                                      ^
              1 error

              Comment

              • abctech
                New Member
                • Dec 2006
                • 157

                #8
                Originally posted by colinNeedsJavaH elp
                Ok so I assigned num1 to be an integer. Then I used this after the input string:

                Code:
                num1 = Integer.parseInt(input.readLine());
                and I get this error:

                Code:
                MagicPrinter.java:24: cannot find symbol
                symbol  : variable input
                location: class MagicPrinter
                num1 = Integer.parseInt(input.readLine());
                                        ^
                1 error
                Have you declared and initialized variable input in your program? If you haven't how will the compiler recognize it ?!

                Comment

                • colinNeedsJavaHelp
                  New Member
                  • Feb 2007
                  • 22

                  #9
                  Originally posted by abctech
                  Have you declared and initialized variable input in your program? If you haven't how will the compiler recognize it ?!

                  I dont really know what the variable input is for.. does that look correct though?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by abctech
                    Have you declared and initialized variable input in your program? If you haven't how will the compiler recognize it ?!
                    Post the code that you have now that is giving this error.

                    Comment

                    • colinNeedsJavaHelp
                      New Member
                      • Feb 2007
                      • 22

                      #11
                      Ok I now get this error:
                      Code:
                      MagicPrinter.java:26: unreported exception java.io.IOException; must be caught or declared to be thrown
                      inputString = br.readLine();
                                               ^
                      1 error
                      This is what I have for my program:
                      Code:
                       
                      import java.io.*;
                      import java.util.*;
                      
                      public class MagicPrinter
                      {
                      public static void main(String[] args)
                      {
                      String inputString;
                      String n1;
                      int num1;
                      char a;
                      a = '*';
                      
                      //for the input stream
                      InputStreamReader isr = new InputStreamReader(System.in);
                      //needed to use ReadLine()
                      BufferedReader br = new BufferedReader(isr);
                      
                      System.out.println("How many lines? ");
                      inputString = br.readLine();
                       
                      StringTokenizer st = new StringTokenizer(inputString);
                      n1 = st.nextToken();
                      
                      num1 = Integer.parseInt(n1);
                      num1 = 0;
                      while(num1<=79) {
                      System.out.println(a);
                      a++;
                      }
                      }
                      }

                      Comment

                      • colinNeedsJavaHelp
                        New Member
                        • Feb 2007
                        • 22

                        #12
                        here is my most recent code:
                        Code:
                        import java.io.*;
                        import java.util.*;
                        
                        public class MagicPrinter
                        {
                        public static void main(String[] args)
                        throws java.io.IOException
                        {
                        String inputString;
                        String n1;
                        int num1;
                        char a;
                        a = '*';
                        //for the input stream
                        InputStreamReader isr = new InputStreamReader(System.in);
                        //needed to use ReadLine()
                        BufferedReader br = new BufferedReader(isr);
                        
                        System.out.println("How many lines? ");
                        inputString = br.readLine();
                        
                        StringTokenizer st = new StringTokenizer(inputString);
                        n1 = st.nextToken();
                        
                        num1 = Integer.parseInt(n1);
                        //double num1 = Double.parseDouble(in.readLine());
                        num1 = 0;
                        while(num1<=79) {
                        System.out.println(a);
                        a++;
                        }
                        }
                        }
                        The program compiles and when run it asks for how many lines.

                        But when you enter a number the program outputs the alphabet then followed by infinite lines of ?

                        Anyone know why?

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by colinNeedsJavaH elp
                          Ok I now get this error:
                          Code:
                          MagicPrinter.java:26: unreported exception java.io.IOException; must be caught or declared to be thrown
                          inputString = br.readLine();
                          ^
                          1 error
                          This is what I have for my program:
                          Code:
                           
                          import java.io.*;
                          import java.util.*;
                           
                          public class MagicPrinter
                          {
                          public static void main(String[] args)
                          {
                          String inputString;
                          String n1;
                          int num1;
                          char a;
                          a = '*';
                           
                          //for the input stream
                          InputStreamReader isr = new InputStreamReader(System.in);
                          //needed to use ReadLine()
                          BufferedReader br = new BufferedReader(isr);
                           
                          System.out.println("How many lines? ");
                          inputString = br.readLine();
                           
                          StringTokenizer st = new StringTokenizer(inputString);
                          n1 = st.nextToken();
                           
                          num1 = Integer.parseInt(n1);
                          num1 = 0;
                          while(num1<=79) {
                          System.out.println(a);
                          a++;
                          }
                          }
                          }
                          You need to put a try-catch block around the code that creates the BufferedReader

                          BufferedReader br null;
                          Code:
                           try { 
                          	   br = new BufferedReader(isr);
                          }
                          catch(IOException iO) {
                          	  iO.printStackTrace();
                          }

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by colinNeedsJavaH elp
                            here is my most recent code:
                            Code:
                            import java.io.*;
                            import java.util.*;
                             
                            public class MagicPrinter
                            {
                            public static void main(String[] args)
                            throws java.io.IOException
                            {
                            String inputString;
                            String n1;
                            int num1;
                            char a;
                            a = '*';
                            //for the input stream
                            InputStreamReader isr = new InputStreamReader(System.in);
                            //needed to use ReadLine()
                            BufferedReader br = new BufferedReader(isr);
                             
                            System.out.println("How many lines? ");
                            inputString = br.readLine();
                             
                            StringTokenizer st = new StringTokenizer(inputString);
                            n1 = st.nextToken();
                             
                            num1 = Integer.parseInt(n1);
                            //double num1 = Double.parseDouble(in.readLine());
                            num1 = 0;
                            while(num1<=79) {
                            System.out.println(a);
                            a++;
                            }
                            }
                            }
                            The program compiles and when run it asks for how many lines.

                            But when you enter a number the program outputs the alphabet then followed by infinite lines of ?

                            Anyone know why?
                            That is what you told it to print

                            while(num1<=79) {
                            System.out.prin tln(a);
                            a++;
                            if a is 'a', a++ makes a 'b'.

                            Do not use char for this. Use string or StringBuilder

                            if you have
                            Code:
                            String string = "*";
                            To add another star you simply do
                            Code:
                             string = string + "*";

                            Comment

                            • colinNeedsJavaHelp
                              New Member
                              • Feb 2007
                              • 22

                              #15
                              for/while loop

                              What is the difference?
                              Last edited by colinNeedsJavaHelp; Feb 26 '07, 08:47 AM. Reason: Trying to delete the post

                              Comment

                              Working...