Solved

break out of a for loop

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

636 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