Solved

break out of a for loop

Posted on 2007-12-01
2
883 Views
Last Modified: 2013-11-23
Hi,

I'm having a problem with a "No Such Element" exception.

I'm itterating a list with a while loop, and within that there's a for loop.

Program enters the while loop at line 2, most of the time the for loop works fine.
but at the end of the list the for loop can not go 15 times when the list has ended and throws the exception.

I thought that as it comes inside a while (i.hasNext()) that it would be ok, but its not.

Can anyone tell me how to load as many characters as are available at the end of the file (when its less that 15) and break out of the for loop and continue without throuwing the exception?

Cheers,

Phil.
Iterator i = result1.iterator();

       while (i.hasNext()){

	for (int x=15; x>0 ; x--){

	c = (char)i.next().hashCode();

	searchword = searchword + c;

	}

			

String temp = searchword;

etc.

Open in new window

0
Comment
Question by:phil8258
2 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 125 total points
ID: 20390218
while (i.hasNext()){          // checks for at least one more item

for (int x=15; x>0 ; x--){  // blindly takes 15 items, without executing the while.

for (int x=15; x>0 &&  i.hasNext(); x--){  // does it better, and now you can drop the while.

Please add code to handle a set of < 15 items.

;JOOP!
0
 

Author Closing Comment

by:phil8258
ID: 31412155
Cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
Responding to Java JComponent extended classes's resize event 4 51
ForLoop Example 3 40
more than one jdk and one jre 1 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now