簡體   English   中英

Java Eclipse中的NoSuchElementException問題

[英]NoSuchElementException problem in java eclipse

我使用Java eclipse,它顯示有:

java.util.NoSuchElementException.

誰能幫助我解決這個問題?

似乎錯誤發生在:

String city = scin3.next();

控制台說:

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

下面是我的代碼:

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 ~");                       
}

}

下面是我的connection.txt文件。

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

你必須檢查有新的令牌來獲得,用hasNext()方法,每次調用之前的next()方法。

就像您可以在next()方法的文檔中看到的那樣:

查找並返回此掃描儀的下一個完整令牌。 完整的標記在其前面,然后是與定界符模式匹配的輸入。 即使先前調用hasNext()返回true,此方法也可能在等待輸入掃描時阻塞。 [...]

NoSuchElementException-如果沒有更多令牌可用

並在hasNext()方法中明確指出:

如果此掃描程序的輸入中包含另一個令牌,則返回true。 等待輸入掃描時,此方法可能會阻塞。 掃描儀不會前進超過任何輸入。

另外,為了改善源代碼,您應該在循環外全部實例化一次Scanner。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM