1

I use Java eclipse and it shows that there is:

java.util.NoSuchElementException.

Can anyone help me with this problem?

It seems that the error occurred at:

String city = scin3.next();

The console says:

java.util.Scanner.throwFor and java.util.Scanner.next.

Below is my code:

package p1;

import java.io.File;
import java.io.IOException;
import java.util.*;

class Connection {
private String departure;
private String arrival;

Connection () {};
Connection (String departure, String arrival) {
    this.departure = departure;
    this.arrival = arrival;
}
String getDeparture() {return departure;}
String getArrival() {return arrival;}
}

public class H4_20160235_1 {
public static void main(String[] args) 
{
    ArrayList<Connection> list = new ArrayList<Connection>();
    File file = new File("connection.txt");
    Scanner scin1;
    if (file.exists()) {
        try {
        scin1 = new Scanner(file);
        while (scin1.hasNext()) {
            String departure = scin1.next();
        String arrival = scin1.next();
        Connection c = new Connection(departure, arrival);
        list.add(c);                        
            }
        scin1.close();
        } catch (IOException e) {}
    }
    else {
        System.out.println("connection.txt not exist!!");
    }

    LinkedList<String> route = new LinkedList<String> ();

    System.out.println("---------------------------------------");
    System.out.println("Welcome to Flight Tour NORANG Ballon !!");
    System.out.println("---------------------------------------");
    System.out.println();

    System.out.println("<<< Flight Information >>>");
    for (Connection l: list) {
        System.out.printf("%s -> %s", l.getDeparture(), l.getArrival());
        System.out.println();
    }
    System.out.println();
    Set<String> set = new LinkedHashSet<String>();
    for (int a=0; a<list.size(); a++) 
        set.add(list.get(a).getDeparture());

    System.out.println("---------------------------------------");
    System.out.println("<<< Cities in the DB >>>");
    System.out.println("---------------------------------------");
    for (String city: set) { 
        System.out.printf("%s", city);
        System.out.println();
    }
    System.out.println("---------------------------------------");
    System.out.println();

    System.out.println("Let's plan a round-trip route!");
    Scanner scin2 = new Scanner(System.in);
    System.out.print("Enter the starting city : ");
    String departure = scin2.next();
    scin2.close();
    route.add(departure);
    System.out.printf("From %s you can fly directly to :", departure);
    System.out.println("\n");

    ArrayList<String> cities = new ArrayList<String> ();
    for (Connection l : list) {
        if (departure.equals(l.getDeparture())) {
            cities.add(l.getArrival());
            System.out.println(l.getArrival());
        }
    }
    System.out.println();
    System.out.println("---------------------------------------");

    int i = 0;
    while (true) {
        Scanner scin3 = new Scanner(System.in);
        System.out.printf("Where do you want to go from %s?", route.get(i));
        String city = scin3.next();
        scin3.close(); 
        i++;
        if (cities.contains(city) == false)
            System.out.println("***** You can't get to that city by a direct flight. *****");
        if (route.contains(city) == true) 
            break;
        ArrayList<String> cities2 = new ArrayList<String>();
        for (Connection l: list) {
            if (city.equals(l.getDeparture())) {
                cities2.add(l.getArrival());
                System.out.println(l.getArrival());
            }
        }
    }

    System.out.println("=====================================");
    System.out.println("<<<  Your Final Route  >>>");
    Iterator <String>iter = route.iterator();
    while (iter.hasNext()) {
        String temp = (String)iter.next();
        System.out.println(temp);
    }
    System.out.println("---------------------------------------");
    System.out.println();
    System.out.println("Have a nice Trip with NORANG Ballon ~");                       
}

}

And below is my connection.txt file.

SanJose SanFrancisco 
SanJose Anchorage 
NewYork Anchorage 
NewYork SanJose 
NewYork SanFrancisco 
NewYork Honolulu 
Anchorage NewYork 
Anchorage SanJose 
Honolulu NewYork 
Honolulu SanFrancisco 
Denver SanJose 
SanFrancisco NewYork
SanFrancisco Honolulu 
SanFrancisco Denver
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
chloe
  • 13
  • 3
  • why are you open and close scanner input in a while and just not open it outside for one time? – Mohsen Dec 09 '18 at 10:49
  • Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Victor10 Dec 09 '18 at 10:54

1 Answers1

0

You must check there is a new token to get, with hasNext() method, before each call to the next() method.

Like you can see in the documentation of next() method:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true. [...]

NoSuchElementException - if no more tokens are available

And specifically stated in the hasNext() method:

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

In addition, to improve your source code, you should instantiate your Scanner once for all, outside your loop.

Community
  • 1
  • 1
Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44