简体   繁体   中英

java.util.NoSuchElementException from Scanner

I'm having trouble with my scanner when it's reading a file. It's suppose to make a new token when there's a comma followed by a space or when a new line is created, but after the 4 tokens, it throws the 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;
}

Here's the file it's trying to read

H, 1.00
O, 16.00

and i made sure there isn't an empty line in the file.

Well im an idiot, my pattern was messed up.

//instead of 
sc.useDelimiter(Pattern.compile("(, ) | (\r\n)"));
//it should be this
sc.useDelimiter(Pattern.compile("(, )|(\r\n)"));

thank you guys for the helpful answers though!

What platform are you using? The line separator differs from platform to platform. Use this for supporting both(and of course remove the extra spaces surrounding the '|' in the regex).

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

I tried running this code on my computer like so:

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;
    }
}

and what I got was

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)

So, it looked like the first match matched the entire file. If you remove the spaces around the pipes:

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;
        }
    }

The message disappears and it works perfectly

I don't like scanner, and avaid it as much as I can. If yo want to try bufferedReader, here's the way to do it:

 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();
} 

EDIT: You can tweak the delimeter in StringTokenizer constructor to suit your needs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM