I need help with breaking out of a loop. I know it's something simple, but I can't remember. Here's my code.

for(count = 0; count < inputArray.length; count++)
{
inputArray[count] = JOptionPane.showInputDialog(
null,
"Please input a name or exit: ",
"Input Name",
JOptionPane.QUESTION_MESSAGE);


if(inputArray[count] == "Exit")
{
break;
}
}

Recommended Answers

All 3 Replies

I don't think you can use the break statement in for loops. I guess you would have to set the count variable to something greater than the ending condition in the loop, that way it won't loop anymore.

for int(i=0; i<10; i++)
{
   if (i=2)
   {
       i = 11;
    }
}

or something like that;

I figured it out. I was comparing strings wrong. Once I fixed that, it worked.

Thanks for the help.

Ok, that was my second guess...
You have to use equals() to compare strings like you were wanting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.