Solved

Why does this give a no such element exception?

Posted on 2007-12-03
3
1,041 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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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