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:
*
**
***
****
*****
******
*******
********
*********
26 6952
here is my first attempt: -
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++);
-
}
-
}
-
}
-
-
-
here is my first attempt: -
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'.This 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.
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'.This 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 -
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);
-
}
-
}
-
-
Yeah I realized after posting I made a ton of errors. Here is what I have now -
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(integers) not strings.
You can simply say: - DataInputStream input = new DataInputStream(System.in);
-
-
System.out.println("How many lines you have to print?");
-
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.
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(integers) not strings.
You can simply say: - DataInputStream input = new DataInputStream(System.in);
-
-
System.out.println("How many lines you have to print?");
-
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
Ok so I assigned num1 to be an integer. Then I used this after the input string: - num1 = Integer.parseInt(input.readLine());
and I get this error: - MagicPrinter.java:24: cannot find symbol
-
symbol : variable input
-
location: class MagicPrinter
-
num1 = Integer.parseInt(input.readLine());
-
^
-
1 error
-
Ok so I assigned num1 to be an integer. Then I used this after the input string: - num1 = Integer.parseInt(input.readLine());
and I get this error: - 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 ?!
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?
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.
Ok I now get this error: - 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: -
-
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++;
-
}
-
}
-
}
-
here is my most recent 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?
Ok I now get this error: - 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: -
-
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; - try {
-
br = new BufferedReader(isr);
-
}
-
catch(IOException iO) {
-
iO.printStackTrace();
-
}
-
here is my most recent 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.println(a);
a++;
if a is 'a', a++ makes a 'b'.
Do not use char for this. Use string or StringBuilder
if you have
To add another star you simply do
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
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!!!!
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.
I gathered someone had.....I just didn't want to look anymore stupis than I already have ;)
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. - 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);
-
a++;
-
-
}
-
}
-
}
Execute the above program first and then read this explanation -
The Ascii value of '*' is 42, so when you say System.out.println(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.
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. -
-
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();
-
}
-
}
-
}
-
Actually instead of assigning "*" to a character I just used it in the output.
Actually instead of assigning "*" to a character I just used it in the output.
num1 = Integer.parseInt(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.print("*");
}
You don't need two for statements.
You just need one for statement that prints the stars like this - 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
-
}
-
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 - 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
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 -
-
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 - System.out.println(stars);
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 -
-
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 - System.out.println(stars);
Just a point of correction -
-
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
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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)
...
|
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...
|
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
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |