not a lot.
Stars pattern program
Collapse
X
-
char and String are different. char is a primitive type while String is class Type.Originally posted by colinNeedsJavaH elpWhat is the difference?
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 dayComment
-
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.
Execute the above program first and then read this explanation -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] } } }
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
-
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(); } } }Comment
-
-
Originally posted by colinNeedsJavaH elpActually instead of assigning "*" to a character I just used it in the output.
That is fine so far. Maybe you want to add error handling (what if the user enters something not a number?)num1 = Integer.parseIn t(br.readLine() );
Now that's very bad. You have just thrown away the value that we want. Remove that linenum1 = 0;
You don't need two for statements.for(i =1; i<num1; i++){
for(j = i; j<0;j--){
System.out.prin t("*");
}
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
-
I took out num1 = 0;Originally posted by r035198xThat 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 }
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 wrongComment
-
Ah so you're online. Won't be long before you complete this now.Originally posted by colinNeedsJavaH elpI 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
No you do not need 2 for loops
Just fill in the codes for lines marked // in the for that I gave you
e.gCode:for(int i = 0;i < num1;i++) { //print stars //print a space //add a star to stars use the + operator //skip a line }
for //print stars, replace with
Code:System.out.println(stars);
Comment
-
Just a point of correctionOriginally posted by r035198xAh 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
e.gCode:for(int i = 0;i < num1;i++) { //print stars //print a space //add a star to stars use the + operator //skip a line }
for //print stars, replace with
Code:System.out.println(stars);
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
Comment