473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Loop Error help?

I'm trying to create a program that creates the following:

*
**
***
****
*****

using loops.

But I keep receiving errors... and I can't figure out why.

Code is as such so far:
Expand|Select|Wrap|Line Numbers
  1. public class Lab7_Ex3
  2. {
  3.     public static void  main(String[] args)
  4.     {
  5.  
  6.  
  7.                 for (int i = 1; i=5; i++);
  8.                 {
  9.                     for (int j = 1;j = 5; j++);
  10.                     {
  11.  
  12.                   System.out.println('*');
  13.                }
  14.                System.out.println();
  15.             }
  16.         }
  17.     }        
  18.  
  19.  
Thank you in advance.
Apr 1 '09 #1
3 1941
JosAH
11,448 Expert 8TB
Decompose your problem; first try to come up with a small method that prints x stars on one line. x is an int parameter to that method:

Expand|Select|Wrap|Line Numbers
  1. public void printStars(int x) { ... }
  2.  
kind regards,

Jos

ps. pay attention to your loop conditions.
Apr 1 '09 #2
ashsa
45
Check the for loop syntax..
The second argument should be an expression that returns a boolean..
In this case, it should be i<=5 instead of i=5.
For the inner loop, j<=5 will be a correct syntax. However, given the requirement, this will not produce the output. Instead, will result in
*****
*****
*****
*****
*****
just think about it..
All the Best !!
Apr 3 '09 #3
dmjpro
2,476 2GB
@Kamaria

What you did in second loop and first loop j=5 and i=5..It shows you the error. Actually j=5 is an assignment operation not a conditional operation. Actually the second statement in for loop must return true or false if the statement exists. But here the statement returns 5.

As Josh said make a method to do this task and call it from main. And take a variable which indicates the no. of rows instead of constant.
Now your first loop is OK if i make it i<=5. But the logical error is in second loop.
See here as the no. of rows increase the column no. is increasing. In each row the column no. is same as row no. So the second loop must be as the current no. of row.
So think a little bit and make the program successful ;)
Apr 4 '09 #4

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

Similar topics

4
by: Ken Fine | last post by:
I'm trying to include a list of people that's the result of looping through a recordset in a CDONTS mail. I'm trying to Dim the output of a loop, and it ain't working -- I'm getting a syntax error....
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
3
by: deko | last post by:
Problem: why doesn't this With block work? is it possible to have a For Each loop within a With block? With objWord .Documents.Add Template:=strTemplate, NewTemplate:=False, DocumentType:=0...
1
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
14
by: dawnerd | last post by:
Hi, I am developing a CMS and came across something which has never happened to me before, and I re-wrote the specific script twice, both differently, and still had the same error. I'm not sure...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
2
by: ocli5568 | last post by:
this main is supposed to receive user input (file names, flags and a keyword), then encrypt the file according to the user input, then close the file pointers. however it goes wrong for some reason....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
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...
0
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...
0
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,...

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.