来自Scanner的java.util.NoSuchElementException

时间:2015-08-18 12:52:19

标签: java java.util.scanner nosuchelementexception

我的扫描仪在读取文件时遇到问题。它假设在一个逗号后跟一个空格或创建一个新行时创建一个新标记,但是在4个标记之后,它会抛出NoSuchElementException。

    private Map<String, Double> createElementMassMap(String filePath) {
    Map<String, Double> elementMap = new HashMap<>();
    try (Scanner sc = new Scanner(new FileReader(filePath))) {
        sc.useDelimiter(Pattern.compile("(, ) | (\r\n)"));
        sc.useLocale(Locale.US);
        while(sc.hasNext()) {
            String name = sc.next();
            System.out.println(name);
            double mass = sc.nextDouble();
            System.out.println(mass);
            elementMap.put(name, mass);
        }
    } catch (IOException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(self, "Error loading ElementMasses file.", "IOException", JOptionPane.ERROR_MESSAGE);
    } 
    return elementMap;
}

这是他尝试阅读的文件

H, 1.00
O, 16.00

我确保文件中没有空行。

4 个答案:

答案 0 :(得分:3)

我是一个白痴,我的模式搞砸了。

{{1}}

谢谢你们有用的答案!

答案 1 :(得分:2)

您使用的是什么平台?行分隔符因平台而异。使用它来支持两者(当然,删除正则表达式中&#39; |&#39;周围的额外空格)。

            sc.useDelimiter("(, )|(\r\n)|(\n)");

答案 2 :(得分:1)

我尝试在我的计算机上运行此代码,如下所示:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Locale;
import java.util.regex.Pattern;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.awt.Component;

public class Test {
    public static void main(String[] args) {
        createElementMassMap("file.txt");
    }

    private static Map<String, Double> createElementMassMap(String filePath) {
        Map<String, Double> elementMap = new HashMap<>();
        try (Scanner sc = new Scanner(new FileReader(filePath))) {
            sc.useDelimiter(Pattern.compile("(, ) | (\r\n) | (\n)"));
            sc.useLocale(Locale.US);
            while(sc.hasNext()) {
                String name = sc.next();
                System.out.println(name);
                System.out.println("hi");
                double mass = sc.nextDouble();
                System.out.println(mass);
                elementMap.put(name, mass);
            }
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog((Component)new Object(), "Error loading ElementMasses file.", "IOException", JOptionPane.ERROR_MESSAGE);
        }
        return elementMap;
    }
}

我得到的是

H, 1.00
O, 16.00

hi
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at Test.createElementMassMap(Test.java:25)
    at Test.main(Test.java:13)

所以,看起来第一场比赛与整个文件相匹配。如果删除管道周围的空格:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Locale;
import java.util.regex.Pattern;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
import java.awt.Component;

public class Test {
    public static void main(String[] args) {
        createElementMassMap("file.txt");
    }

        private static Map<String, Double> createElementMassMap(String filePath) {
            Map<String, Double> elementMap = new HashMap<>();
            try (Scanner sc = new Scanner(new FileReader(filePath))) {
                sc.useDelimiter(Pattern.compile("(, ) | (\r\n) | (\n)"));
                sc.useLocale(Locale.US);
                while(sc.hasNext()) {
                    String name = sc.next();
                    System.out.println(name);
                    System.out.println("hi");
                    double mass = sc.nextDouble();
                    System.out.println(mass);
                    elementMap.put(name, mass);
                }
            } catch (IOException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog((Component)new Object(), "Error loading ElementMasses file.", "IOException", JOptionPane.ERROR_MESSAGE);
            }
            return elementMap;
        }
    }

消息消失,效果很好

答案 3 :(得分:1)

我不喜欢扫描仪,并且尽可能地使用扫描仪。 如果你想尝试bufferedReader,这就是这样做的方法:

 BufferedReader in = new BufferedReader(new FileReader(filePath));
   StringTokenizer st;
   String line;

   try {
    while((line = in.readLine()) != null) {
           st = new StringTokenizer(line,", " );
           String name = st.nextToken();
           System.out.println(name);
           double mass = Double.parseDouble(st.nextToken());
           System.out.println(mass);
       }
} catch (Exception e) {
    // kaboom... something happened
    e.printStackTrace();
} 

编辑: 您可以在StringTokenizer构造函数中调整分隔符以满足您的需要