File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Changing for loop into a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing for loop into a while loop" Watch "Changing for loop into a while loop" New topic
Author

Changing for loop into a while loop

Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Shawn Houston
Greenhorn

Joined: Oct 14, 2004
Posts: 22
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing for loop into a while loop