Solved

break out of a for loop

Posted on 2007-12-01
2
882 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

759 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

17 Experts available now in Live!

Get 1:1 Help Now