2

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.

Alex S.
  • 53
  • 5

4 Answers4

3

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!

Alex S.
  • 53
  • 5
2

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)");
Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
1

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

1

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

Tawcharowsky
  • 615
  • 4
  • 18