954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Breaking out of a Loop

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;
}
}

Lost Chyld
Newbie Poster
6 posts since Mar 2004
Reputation Points: 12
Solved Threads: 0
 

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;

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

Thanks for the help.

Lost Chyld
Newbie Poster
6 posts since Mar 2004
Reputation Points: 12
Solved Threads: 0
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You