[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Why does this give a no such element exception?

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

650 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