Solved

Why does this give a no such element exception?

Posted on 2007-12-03
3
998 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 125 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 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

15 Experts available now in Live!

Get 1:1 Help Now