Stars pattern program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #16
    not a lot.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #17
      Originally posted by colinNeedsJavaH elp
      What is the difference?
      char and String are different. char is a primitive type while String is class Type.
      char can only hold one char value. You cannot use char to represent

      ****. For that you need the String type. You may want to find some time to read this one day

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #18
        and for the record (before everyone jumps in)....the "not a lot" was in response to a thread entitled "a" that said "what's the difference" without the preceding discussion.....
        My Bad!!!!

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #19
          Originally posted by DeMan
          and for the record (before everyone jumps in)....the "not a lot" was in response to a thread entitled "a" that said "what's the difference" without the preceding discussion.....
          My Bad!!!!
          I just merged the threads.

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #20
            I gathered someone had.....I just didn't want to look anymore stupis than I already have ;)

            Comment

            • abctech
              New Member
              • Dec 2006
              • 157

              #21
              Hi,
              Couldn't resist, remembered the times when I just started with programming and would be equally confused about these * programs.

              OP,
              I've used Thread.sleep() so you can actually see what output you are getting due to using char a = '*' ; This method will slow your output a bit and allow you to take a good look at every line.

              Code:
              import java.io.*;
              import java.util.*;
               
              public class MagicPrinter
              {
              	public static void main(String[] args)throws IOException,InterruptedException
              	{
              		String inputString;
              		String n1;
              		int 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();
               
              		StringTokenizer st = new StringTokenizer(inputString);
              		n1 = st.nextToken();
              		System.out.println("n1: " + n1);		
               
              		num1 = Integer.parseInt(n1);
              		System.out.println("num1: " + num1);
              		num1 = 0;
              		while(num1<=79) 
              		{
              			System.out.println(a);
              			Thread.sleep(200);
              			[B]a++;[/B]
              
              		}
              	}
              }
              Execute the above program first and then read this explanation -

              The Ascii value of '*' is 42, so when you say System.out.prin tln(a) in your while loop, you will get a star in the first line of your output which is fine, but then in the very next line of your while by saying a++ you are actually incrementing the ascii value internally. So now the value has become 43 and 43 represents a '+' which will be the o/p on the second line. Then again the while loop proceeds executing a++ thus making a = 44 which represents a ',' which is the output on your third line..and so on..Thats why your program outputs a series of characters followed by infinite lines of ?....

              Hope you see the output of the above program first, understand it and then change your logic accordingly. We will keep giving you pointers wherever you need help.

              Comment

              • colinNeedsJavaHelp
                New Member
                • Feb 2007
                • 22

                #22
                Ok so I got the program how it should be the only problem is that it doesnt produce the right output. When ran it prompts the user "How many lines?" But then when you put in a number it doesnt do anything, the cursor just moves to the next line and stays there. I have to turn this in tomorrow morning so if anyone knows what is going wrong I would appreciate the help. I also changed it to a for loop. The part where the mistake is has to be in the loop but I can't for the life of me figure it out.

                Code:
                 
                import java.io.*;
                import java.util.*;
                
                public class MagicPrinter
                {
                public static void main(String[] args)
                throws java.io.IOException
                {
                String inputString;
                int num1;
                int a =1;
                int i;
                int j;
                
                //for the input stream
                InputStreamReader isr = new InputStreamReader(System.in);
                
                //needed to use ReadLine()
                BufferedReader br = new BufferedReader(isr);
                
                System.out.print("How many lines? ");
                inputString = br.readLine();
                
                num1 = Integer.parseInt(br.readLine());
                num1 = 0;
                for(i =1; i<num1; i++){
                for(j = i; j<0;j--){   
                System.out.print("*");
                }
                System.out.println();
                }
                }
                }
                Last edited by colinNeedsJavaHelp; Feb 27 '07, 11:44 PM. Reason: clarification

                Comment

                • colinNeedsJavaHelp
                  New Member
                  • Feb 2007
                  • 22

                  #23
                  Actually instead of assigning "*" to a character I just used it in the output.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #24
                    Originally posted by colinNeedsJavaH elp
                    Actually instead of assigning "*" to a character I just used it in the output.

                    num1 = Integer.parseIn t(br.readLine() );
                    That is fine so far. Maybe you want to add error handling (what if the user enters something not a number?)

                    num1 = 0;
                    Now that's very bad. You have just thrown away the value that we want. Remove that line


                    for(i =1; i<num1; i++){
                    for(j = i; j<0;j--){
                    System.out.prin t("*");
                    }
                    You don't need two for statements.

                    You just need one for statement that prints the stars like this

                    Code:
                     String stars = "*"; 
                    for(int i = 0;i < num1;i++) {
                    	//print stars
                    	//print a space
                    	//add a star to stars use the + operator
                    	//skip a line
                    }

                    Comment

                    • colinNeedsJavaHelp
                      New Member
                      • Feb 2007
                      • 22

                      #25
                      Originally posted by r035198x
                      That is fine so far. Maybe you want to add error handling (what if the user enters something not a number?)


                      Now that's very bad. You have just thrown away the value that we want. Remove that line




                      You don't need two for statements.

                      You just need one for statement that prints the stars like this

                      Code:
                       String stars = "*"; 
                      for(int i = 0;i < num1;i++) {
                      	//print stars
                      	//print a space
                      	//add a star to stars use the + operator
                      	//skip a line
                      }
                      I took out num1 = 0;

                      But I have to keep the 2 "for" statements so it does a loop. How can I get it to work? I cannot find anything wrong

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #26
                        Originally posted by colinNeedsJavaH elp
                        I took out num1 = 0;

                        But I have to keep the 2 "for" statements so it does a loop. How can I get it to work? I cannot find anything wrong
                        Ah so you're online. Won't be long before you complete this now.

                        No you do not need 2 for loops
                        Just fill in the codes for lines marked // in the for that I gave you
                        Code:
                         
                        for(int i = 0;i < num1;i++) {
                        //print stars
                        //print a space
                        //add a star to stars use the + operator
                        //skip a line
                        }
                        e.g
                        for //print stars, replace with

                        Code:
                        System.out.println(stars);

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #27
                          Originally posted by r035198x
                          Ah so you're online. Won't be long before you complete this now.

                          No you do not need 2 for loops
                          Just fill in the codes for lines marked // in the for that I gave you
                          Code:
                           
                          for(int i = 0;i < num1;i++) {
                          //print stars
                          //print a space
                          //add a star to stars use the + operator
                          //skip a line
                          }
                          e.g
                          for //print stars, replace with

                          Code:
                          System.out.println(stars);
                          Just a point of correction


                          Code:
                           
                          for(int i = 0;i < num1;i++) {
                          //print stars
                          //add a space to stars use the + operator
                          //add a star to stars use the + operator
                          //skip a line
                          }

                          Comment

                          Working...