Hello all,
Needing a little assistance with a while-loop project.
I'm having to convert a for-loop into a while loop, but that's not what's giving me a hard time.
Here's the for-loop:
and here's the for-loop converted to a while-loop:
I know that part is right (is it though?), where I'm running into trouble is plugging it into the code:
I commented out the area that I am having trouble with. Can someone give me some direction on plugging this while-loop statement into this code? I'm using netbeans IDE if it matters.
This is what I plug into the commented portion of the code:
And it gives me an error "Break outside switch or loop"
Edited by Krunk_Kracker - 7/20/12 at 9:11am
Needing a little assistance with a while-loop project.
I'm having to convert a for-loop into a while loop, but that's not what's giving me a hard time.
Here's the for-loop:
Code:
for (int i=0; i<names.length; i++){
if (names [i ].equals(searchName)){
foundName = true;
break;
and here's the for-loop converted to a while-loop:
Code:
int i = 0;
while( i < names.length);
i++;
if (names [i ].equals(searchName)){
foundName = true;
break;
I know that part is right (is it though?), where I'm running into trouble is plugging it into the code:
Code:
import javax.swing.JOptionPane;
public class MyOwnWhileProject {
public MyOwnWhileProject() {
}
public static void main(String[] args) {
String names []={"Beah","Bianca","Lance","Belle","Nico","Yza","Gem","Ethan"};
/**
for (int i=0; i<names.length; i++){
if (names [i ].equals(searchName)){
foundName = true;
break;
*/
}
}
if (foundName)
JOptionPane.showMessageDialog(null, searchName + " is found!");
else
JOptionPane.showMessageDialog(null, searchName + " is not found!");
}
}
I commented out the area that I am having trouble with. Can someone give me some direction on plugging this while-loop statement into this code? I'm using netbeans IDE if it matters.
This is what I plug into the commented portion of the code:
Code:
int i = 0;
while( i < names.length);
i++;{
if (names [i ].equals(searchName)){
foundName = true;
break;
And it gives me an error "Break outside switch or loop"
Edited by Krunk_Kracker - 7/20/12 at 9:11am









