Solved

break out of a for loop

Posted on 2007-12-01
2
891 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
collection output issue 9 91
Tagging and Merging on Branch 1 68
DTD and JAVA versions 1 55
How do i find out all classes which contain nested class 2 14
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 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