-3

I am trying to read coordinates from a file. The format is start point, end point, polygons. Here is an example:

1, 3
34, 19
0, 14; 6, 19; 9, 15; 7, 8; 1, 9
2, 6; 17, 6; 17, 1; 2, 1

I read each line using a BufferedReader and have confirmed that is working. I am trying to use Scanner to parse each line for the numbers as doubles, so I use Scanner's nextDouble method, but when I run the code it throws a InputMismatchException on the first read. Here is the logic of those lines:

File line - "1, 3"

Code: lineReader.nextDouble();

Any ideas?

Ashish Kumar
  • 916
  • 2
  • 15
  • 32
Pareod
  • 151
  • 1
  • 11
  • 1
    where is your code. give us some important details. – msagala25 Feb 16 '17 at 03:25
  • The code is a few hundreds of lines long and wouldn't be very useful here, but I'll add a bit to the question. – Pareod Feb 16 '17 at 03:30
  • These hundreds of lines are almost always can be compressed into [MCVE](http://stackoverflow.com/help/mcve), which may or may not be related to the original code. – yeputons Feb 16 '17 at 03:36
  • 1
    I believe that `Scanner` assumes that its input is separated with whitespaces, so you have to either alter its behavior (if possible) or read these commans/semicolons manually. Say, if you call `nextDouble()`, it reads `1`, then you call it one more time, it will probably fail because it can only read `,`. – yeputons Feb 16 '17 at 03:37
  • Another probable reason is `,` being decimal separator in your locale, so `Scanner` is confused by `,` followed with space. – yeputons Feb 16 '17 at 03:38
  • I thought this might be the case, so I tried to use a delimiter on commas and semi-colons like so" `lineReader.useDelimiter([,;]);` but that didn't work. I'm not sure if that's how you do it though. – Pareod Feb 16 '17 at 03:40

2 Answers2

0

Kindly refer to Scanner class. Its having example of, how to use delimiters with Scanner.

Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");

Below is the example which is reading your input from a file.

public static void main(String[] args) {
    try (Scanner in = new Scanner(new File("D:/INPUT_OUTPUT/Data_Stack.txt"))) {

        // Adding delimiters.
        in.useDelimiter("\\s*,\\s*|\\s*\\n\\s*|\\s*;\\s*");

        // Reading files.
        while (in.hasNextDouble()) {
            System.out.println(in.nextDouble());
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Data in the file (Data_Stack.txt)-:

1, 3
34, 19
0, 14; 6, 19; 9, 15; 7, 8; 1, 9
2, 6; 17, 6; 17, 1; 2, 1

Output-:

1.0
3.0
34.0
19.0
0.0
14.0
6.0
19.0
9.0
15.0
7.0
8.0
1.0
9.0
2.0
6.0
17.0
6.0
17.0
1.0
2.0
1.0

Description-: From Scanner we can see that-:

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

So we have to add required delimiter to read our data. Required delimiters are ,, \n and ; which have been added in line in.useDelimiter("\\s*,\\s*|\\s*\\n\\s*|\\s*;\\s*"); of code.
After adding delimiters to Scanner its reading data correctly.

Ashish Kumar
  • 916
  • 2
  • 15
  • 32
  • I have read through the delimiter example, but it doesn't really explain it too well. Is the extra `\\`` in `\\s*` a break character? I'm assuming `|` means OR. Why do we need any number of white spaces between `,`, `;`, and `\n`? Also, how the heck do you just write the break character in code quotes haha – Pareod Feb 17 '17 at 18:05
-1

Scanner.next() is '1,',the type is String.

Try below code:

    while(scanner.hasNext()){
        if (scanner.hasNextDouble()) {
            System.out.println("double="+scanner.nextDouble());
        }else{
            System.out.println("string="+scanner.next());
        }
    }

Then you can understand.

Tom Grylls
  • 121
  • 7