?
Solved

Why does this give a no such element exception?

Posted on 2007-12-03
3
Medium Priority
?
1,058 Views
Last Modified: 2012-06-21
I'm not sure why this is giving a noSuchElementException. I've been starring at it forever and can't figure it out.


Input File

Blue             Gold            500
Gold             Red             300
Magenta          Yellow          438
Blue             Yellow          531
Magenta          Blue            865
Red              Purple          765
Blue             Purple          387
Brown            Magenta         1100
Brown            Blue            435
Gold             Silver          654
Gold             Purple          186


Code

package pipelines;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class MainClass
{
      public static  void main(String[] args) throws FileNotFoundException, NoSuchElementException
      {
            String filename = "blackgold.txt";
            Scanner scan = new Scanner(new File(filename));
            
            while(scan.hasNextLine())
            {
                  String mine1 = scan.next();
                  String mine2 = scan.next();
                  int distance = scan.nextInt();
                  
                  System.out.println(mine1 + " " + mine2 + " " + distance);
            }
            scan.close();
      }
}
0
Comment
Question by:toymachiner62
3 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
ID: 20401245
>>String mine1 = scan.next();
                  String mine2 = scan.next();
                  int distance = scan.nextInt();
>>

change to,

String mine1 = scan.hasNext() ? scan.next() : "";
String mine2=scan.hasNext()? scan.next() : "";
int distance = scan.hasNextInt()?scan.nextInt():0;
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20401248
you dont need NoSuchElementException, you just need FileNotFoundException
0
 
LVL 92

Expert Comment

by:objects
ID: 20401249
u need to cvall nextLine()
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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