Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing for loop into a while loop

 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have a program here that needs the outer for loop changed to a while loop , and the first inner for loop changed to a do..while loop. Now, I got the inner loop changed and it works fine, IF I don't change the outter loop at all. Heres the code:

for (int x = 1; x <= height; x++)
{
if (x == 1 || x == height)
{
y = 1;
do
{
y = y + 1;
System.out.print(rectangle);
}
while(y <= width);
}
else{
System.out.print(rectangle);
for (int w = 2; w <= width-1; w++){
System.out.print(" ");}

System.out.println(rectangle);
}
System.out.println();
}

}

}

Now the first inner for loop has already been changed. The outer loop which is the for(int x = 1; x <= height; x++), is the one that needs to be changed to a while loop.

This is what I'm figuring and I dont see where I'm going wrong but i get all sorts of 'class' and 'interface' expected errors.

int x;

x = 1;
while(x <= height)
{
if(x == 1 || x == height)
}
x++;
}
(next while loop)

Anyone able to give me suggestions on what I'm doing wrong without fixing my code?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got some extra right-curly-braces in there, which ends up prematurely closing the "class" scope. Therefore when the compiler sees the line it's complaining about, it was expecting to see the beginning of another class, not a continuation of the previous one.

This is the kind of thing that a good programmer's editor or IDE can really help you with; I'm betting you're editing code in "notepad" or something similar. A programmer's editor can show you matching braces and parens, and also format your code for you, taking braces and parens into account. If they don't match, the formatting will be wrong, making the mistake obvious.
 
Shawn Houston
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where would i find a good IDE at?.

I figured it was something to do with my braces. I changed the code now and it runs but it doesnt finish because it goes into an infinite loop. If i put a { or } at the end of the first while statement i get the same 'class' errors, and I'm not sure what to do from here.


x = 1;
while(x <= height)
{
if (x == 1 || x == height)
{
x++;
<==== heres where the infinite loop and my 'class' errors
y = 1; are occuring.
do
{
y = y + 1;
System.out.print(rectangle);
}
while(y <= width);
}
else{
System.out.print(rectangle);
for (int w = 2; w <= width-1; w++){
System.out.print(" ");}

System.out.println(rectangle);
}
System.out.println();
}

}

}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, realize that any for loop can be turned into a while loop:becomesFor example, you have moved the <increment> for the outer loop, x++, inside an if-block that is part of <loop>. Thus it only increments the loop counter when x is 1 or height, but you need it to increment it every time.

The reason you're encountering an infinite loop is that x is incremented only the first time through the loop, from 1 to 2. From then on the else block is executed and x is left as-is. Thus the outer test never fails.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shawn Houston:
Where would i find a good IDE at?.


There are many good choices. The most popular free Java IDE is Eclipse, available at http://www.eclipse.org. You can also head over to our IDEs and other tools forum and ask for recommendations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic