Some Java Programming help..

Muhammad.R1

Active Member
Joined
Sep 8, 2007
Messages
67
Reaction score
1
Hey there guys , i've been doing I.T at school and we've been doing java programming with jGrasp.

But today the most awkward thing happened , I totally forgot the basic logic behind a program i wrote last year and im struggling with it for some reason. I've done the basics and just finished with string manipulation and about to move on to netbeans and just went over some pass work and have already forgotten it :S.

Code:
int height=scan.nextInt();

for(int i=0;i<height;i++)
{
for(int j=0;j<i;j++)
{
System.out.print("*");
}
System.out.println("*");
}

So basically thats the code and it prints out this triangle when 5 is entered:

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

Now for some reason i am having a problem understanding how this for loop is working...

This is from my own understanding ( which is wrong no doubt).
The first star is printed by the first for loop because the second loop doesn't run since 0<0 doesn't work right? So "*" is printed and i becomes 1. Then it runs through again and goes through the second loop...so 0<1 ,and it prints out a "*". But , that can't be right since that would mean that the second star is printed next to the first star by the first loop so **.....but it has *...lol:confused:

Sorry , I'll probably confuse you guys too :D

Any help would be much appreciated thanks ^^!

P.S - If anyone knows a good website or a ebook/book (besides the school text book) to help further with java programming i'd really appreciate it.
 
I really wish I could explain a bit better but it is just logic to me and makes sense to me.. You need to follow the loop through using a flow table of the variables I and j.

Don't forget that println ends the line and print doesn't..
 
First "*" is printed by "System.out.println("*");" when i=0.
 
First "*" is printed by "System.out.println("*");" when i=0.
Right so its obviously apparent that im having trouble understanding how the nested for loop works , so when that first * is printed its on its on line. Then afterwards the second for loop prints a star and another one soon follows so it becomes :
*
**
But then how is the 3+ in one line printed because this is what im thinking of..
i=0;j=0 > *
i=1;j=0 > **
i=2;j=1 > **
 
Hey there guys , i've been doing I.T at school and we've been doing java programming with jGrasp.

But today the most awkward thing happened , I totally forgot the basic logic behind a program i wrote last year and im struggling with it for some reason. I've done the basics and just finished with string manipulation and about to move on to netbeans and just went over some pass work and have already forgotten it :S.

Code:
int height=scan.nextInt();

for(int i=0;i<height;i++)
{
for(int j=0;j<i;j++)
{
System.out.print("*");
}
System.out.println("*");
}

So basically thats the code and it prints out this triangle when 5 is entered:

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

Now for some reason i am having a problem understanding how this for loop is working...

This is from my own understanding ( which is wrong no doubt).
The first star is printed by the first for loop because the second loop doesn't run since 0<0 doesn't work right? So "*" is printed and i becomes 1. Then it runs through again and goes through the second loop...so 0<1 ,and it prints out a "*". But , that can't be right since that would mean that the second star is printed next to the first star by the first loop so **.....but it has *...lol:confused:

Sorry , I'll probably confuse you guys too :D

Any help would be much appreciated thanks ^^!

P.S - If anyone knows a good website or a ebook/book (besides the school text book) to help further with java programming i'd really appreciate it.
The best e-book I have seen for someone wishing to revise Java is called "Introduction to Java Programming, 8th Ed" by Y. Daniel Liang.

It's a university text, and his explanations and diagrams are very good. It comes in two versions, one comprehensive, and covers Java 7.
 
I also recommend doing a manual trace to follow the nested looping.
 
Hey there guys , i've been doing I.T at school and we've been doing java programming with jGrasp.

But today the most awkward thing happened , I totally forgot the basic logic behind a program i wrote last year and im struggling with it for some reason. I've done the basics and just finished with string manipulation and about to move on to netbeans and just went over some pass work and have already forgotten it :S.

Code:
int height=scan.nextInt();

for(int i=0;i<height;i++)
{
for(int j=0;j<i;j++)
{
System.out.print("*");
}
System.out.println("*");
}

So basically thats the code and it prints out this triangle when 5 is entered:

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

Now for some reason i am having a problem understanding how this for loop is working...

This is from my own understanding ( which is wrong no doubt).
The first star is printed by the first for loop because the second loop doesn't run since 0<0 doesn't work right? So "*" is printed and i becomes 1. Then it runs through again and goes through the second loop...so 0<1 ,and it prints out a "*". But , that can't be right since that would mean that the second star is printed next to the first star by the first loop so **.....but it has *...lol:confused:

Sorry , I'll probably confuse you guys too :D

Any help would be much appreciated thanks ^^!

P.S - If anyone knows a good website or a ebook/book (besides the school text book) to help further with java programming i'd really appreciate it.

Remember doing the same example. :) Its straight forward. Do a tracetable, give your variables meaningful names e.g. "outerLoop" and "innerLoop", remember the difference between print and println. (Hereafter is just guess work with out looking at the problem) The outer loop controls the number of *s per line and the inner loop controls the movement to the next line. The part you're getting confused about is the movement to the next line. Notice the
System.out.print("*");
part. That shows that it will print a * without moving to the next line. If this runs twice because of the loop, you will have **. This will continue until the loop completes.
 
Remember doing the same example. :) Its straight forward. Do a tracetable, give your variables meaningful names e.g. "outerLoop" and "innerLoop", remember the difference between print and println. (Hereafter is just guess work with out looking at the problem) The outer loop controls the number of *s per line and the inner loop controls the movement to the next line. The part you're getting confused about is the movement to the next line. Notice the part. That shows that it will print a * without moving to the next line. If this runs twice because of the loop, you will have **. This will continue until the loop completes.

I disagree with this... The inner loop is what controls the number of *s per line and the outer loop controls the movement to the next line.
 
I disagree with this... The inner loop is what controls the number of *s per line and the outer loop controls the movement to the next line.

Ah, thanks for that. I just remember that the two functions were used.. Don't remember which was which and as I said, that was a guess.
 
I find understanding code much easier if its properly indented. Are you working with it properly indented, because I know someone complained that mybb removes indentation when pasting code..
 
There should only be one Print statement... inside the inner loop...

The best to see what it does and to understand it - is to grab a pen and paper and manually follow the instructions in your code.
One of the previous posters also mentioned this.
 
they like to make confusing tasks it seems :)

Code:
String star = "*";
String line = "";
int height=scan.nextInt();
for (int i=0; i<height; i++) {
	line += star;
	System.out.println(line);
}
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X