?
Solved

Why does this give a no such element exception?

Posted on 2007-12-03
3
Medium Priority
?
1,047 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
[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
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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