Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

break out of a for loop

Posted on 2007-12-01
2
Medium Priority
?
895 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

916 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