473,609 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stars pattern program

22 New Member
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:
*
**
***
****
*****
******
*******
********
*********
Feb 25 '07 #1
26 6976
colinNeedsJavaHelp
22 New Member
here is my first attempt:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. publc class MagicPrinter
  5. {
  6. publc static void main(String[] args);
  7. {
  8. String inputString;
  9. double num1;
  10. char a = "*";
  11.  
  12. //for the input stream
  13. InputStreamReader isr = new InputStreamReader(System.in);
  14. //needed to use ReadLine()
  15. BufferedReader br = new BufferedReader(isr);
  16.  
  17. System.out.println("How many lines? ";
  18. inputString = br.readLine();
  19.  
  20. do{
  21. for(num 1 == 1;num1>3; a++){
  22.   for(num1 ==1;num3<80;a++){
  23.   System.out.print(a++);
  24.  }
  25.    }
  26.     }
  27.  
  28.  
  29.  
Feb 25 '07 #2
abctech
157 New Member
here is my first attempt:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. publc class MagicPrinter
  5. {
  6. publc static void main(String[] args);
  7. {
  8. String inputString;
  9. double num1;
  10. char a = "*";
  11.  
  12. //for the input stream
  13. InputStreamReader isr = new InputStreamReader(System.in);
  14. //needed to use ReadLine()
  15. BufferedReader br = new BufferedReader(isr);
  16.  
  17. System.out.println("How many lines? ";
  18. inputString = br.readLine();
  19.  
  20. do{
  21. for(num 1 == 1;num1>3; a++){
  22.   for(num1 ==1;num3<80;a++){
  23.   System.out.print(a++);
  24.  }
  25.    }
  26.     }
  27.  
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.
Feb 25 '07 #3
colinNeedsJavaHelp
22 New Member
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
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MagicPrinter
  5. {
  6. public static void main(String[] args)
  7. {
  8. String inputString;
  9. String = num1;
  10. char a;
  11. a = '*';
  12.  
  13. //for the input stream
  14. InputStreamReader isr = new InputStreamReader(System.in);
  15. //needed to use ReadLine()
  16. BufferedReader br = new BufferedReader(isr);
  17.  
  18. System.out.println("How many lines? ");
  19. num1 = br.readLine();
  20.  
  21. //double num1 = Double.parseDouble(in.readLine());
  22. num1 = 0;
  23. do{
  24. System.out.println(a);
  25. a++;
  26. }while(num1<80);
  27. }
  28. }
  29.  
  30.  
Feb 25 '07 #4
abctech
157 New Member
Yeah I realized after posting I made a ton of errors. Here is what I have now
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MagicPrinter
  5. {
  6. public static void main(String[] args)
  7. {
  8. String inputString;
  9. String = num1;
  10. char a;
  11. a = '*';
  12.  
  13. //for the input stream
  14. InputStreamReader isr = new InputStreamReader(System.in);
  15. //needed to use ReadLine()
  16. BufferedReader br = new BufferedReader(isr);
  17.  
  18. System.out.println("How many lines? ");
  19. num1 = br.readLine();
  20.  
  21. //double num1 = Double.parseDouble(in.readLine());
  22. num1 = 0;
  23. do{
  24. System.out.println(a);
  25. a++;
  26. }while(num1<80);
  27. }
  28. }
  29.  
  30.  
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:
Expand|Select|Wrap|Line Numbers
  1. DataInputStream input = new DataInputStream(System.in);
  2.  
  3. System.out.println("How many lines you have to print?");
  4. int num1  = 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:

http://www.thescripts.com/forum/thread598625.html
http://www.thescripts.com/forum/thread596997.html

Have a look and then try again.
Feb 25 '07 #5
colinNeedsJavaHelp
22 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. DataInputStream input = new DataInputStream(System.in);
  2.  
  3. System.out.println("How many lines you have to print?");
  4. int num1  = 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:

http://www.thescripts.com/forum/thread598625.html
http://www.thescripts.com/forum/thread596997.html

Have a look and then try again.
I want to print lines between odd numbers 3 and 79
Feb 25 '07 #6
colinNeedsJavaHelp
22 New Member
Ok so I assigned num1 to be an integer. Then I used this after the input string:

Expand|Select|Wrap|Line Numbers
  1. num1 = Integer.parseInt(input.readLine());
and I get this error:

Expand|Select|Wrap|Line Numbers
  1. MagicPrinter.java:24: cannot find symbol
  2. symbol  : variable input
  3. location: class MagicPrinter
  4. num1 = Integer.parseInt(input.readLine());
  5.                         ^
  6. 1 error
  7.  
Feb 25 '07 #7
abctech
157 New Member
Ok so I assigned num1 to be an integer. Then I used this after the input string:

Expand|Select|Wrap|Line Numbers
  1. num1 = Integer.parseInt(input.readLine());
and I get this error:

Expand|Select|Wrap|Line Numbers
  1. MagicPrinter.java:24: cannot find symbol
  2. symbol  : variable input
  3. location: class MagicPrinter
  4. num1 = Integer.parseInt(input.readLine());
  5.                         ^
  6. 1 error
  7.  
Have you declared and initialized variable input in your program? If you haven't how will the compiler recognize it ?!
Feb 25 '07 #8
colinNeedsJavaHelp
22 New Member
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?
Feb 25 '07 #9
r035198x
13,262 MVP
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.
Feb 26 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

12
12062
by: Miha Kovac | last post by:
Hi, can anybody help me with this one please. How can I make a glittering stars effect for my background? Please help, TIA M
3
5106
by: kittykat | last post by:
Hi, I was wondering if you could help me. I am writing a program in C++, and the problem is, i have very limited experience in this language. I would like my user to enter a specific pattern, and I want my program to search a text file for this pattern, and let the user know if this pattern exists or not. So far, i have figured out how to make my prgram read the text file, but i'm not sure how to take the information the user inserts...
6
2322
by: pitachu | last post by:
Hi, I'm not an expect in .NET, so would anyone know an answer a design pattern for the following? There are many customers that require minor customizations to the program I will be developing. I would like to reuse the majority of the functionality of this program since each customer is only requiring certain changes to this program.
34
11170
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
11
2608
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) printf($); Code below will take the size of what I input and change the size of the pattern by adding more or less $
1
1728
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that had no connection or reference to the object that triggered it. It goes something like this: (not syntactically correct..it's just for the idea)
2
1523
by: darween | last post by:
How To write a full program to ask the users to enter a number , which indicates the number / level of stars that he / she wishes to display
2
2048
by: hogcia | last post by:
Hello! I've got a little problem - I'm writing a program in C++, which should compare a text input from keyboard with a regular expression and return what parttern was recognized and where. The problem is the program doesn't find a pattern even if it actually is in the string. This is the code: #include <regex.h> #include <iostream> #include <string>
0
1024
by: bs866806 | last post by:
Why do many people look to movie stars for answers to some of life's most challenging questions? While we have great respect for the art of acting, as explicated from Stanislavsky to Strasberg, the latter of whom we knew well and were fond of, we have never understood how the usual snippets who decide to become actors ascend in the minds of the public from being initially generally regarded as likely ne'er-do-wells to being considered...
0
8139
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8555
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8408
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5524
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.