1

I am using Java (VSC is my compiler). I try to read through a document that is in the same folder. However, just scanning causes this error:

An error has occured.
java.util.InputMismatchException
        at java.base/java.util.Scanner.throwFor(Scanner.java:939)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
        at Project1.results(Project1.java:66)
        at Project1.main(Project1.java:87)

This is my whole code:

public class Project1() {

    public void results(String fileName){


            double x, y, xc, yc, rad, radius; 

            int number_of_circles = 0;
            try {
              Scanner scanner = new Scanner(new BufferedReader(new FileReader(fileName))); 

              while(scanner.hasNext()) {

                x = scanner.nextDouble();
                y = scanner.nextDouble();
                rad = scanner.nextDouble();
                if(rad > 0) { 
                    number_of_circles++;
                }
              }

            } 
            catch(Exception exception) {
              System.err.println("An error has occured."); 
              exception.printStackTrace();
            }

    }
    public static void main(String args[]){
        Project1 P = new Project1();
        P.results("Project1.data"); 

    }
}

I tried different files with different values but it does not seem to help. Thanks. I looked at other threads, but they seem to not cover the exact same problem. It looks like if I put only integer values inside Project1.data, then it works, but obviously I want to allow for other values

Project1.data values:

9.50 2.40 3.20
2.20 3.40 5.60
2.50 2.40 3.20
3.20 4.40 5.60 
ozajasz15
  • 39
  • 6
  • The file 'Project1.data' does not start with 3 whitespace-separated double values. – rzwitserloot Feb 22 '20 at 21:22
  • I wrote in the post how project1.data looks like. Should I separate it in a different way? Between every 3 values, there is a space and after every set there is an enter – ozajasz15 Feb 22 '20 at 21:27

1 Answers1

2

Is it possible that your locale expects , as separator, while your file contains . ?

Related question: Best way to parseDouble with comma as decimal separator?

Here is another related question with tips for Scanner: Java - Scanning comma delimited double and int values

stepio
  • 865
  • 2
  • 9
  • 22
  • Yes, you are right! I would not think about my VSC using , as a separator. Why does it happen? Can I change it in any way? – ozajasz15 Feb 22 '20 at 21:54
  • Updated my answer with another link - specifically for Scanner. This should help you. In short: start with defining another Locale for your Scanner instance - ENGLISH should work, for example. – stepio Feb 22 '20 at 21:58
  • Thank you. One last question - is it a problem of my locale or is it going to happen by default? This question is a part of my homework, so I am not sure if only I will have this problem on my computer, or maybe my lecturer will have a problem with running my code. – ozajasz15 Feb 22 '20 at 22:00
  • 1
    Usually it's country- and language-specific. If you hard-code (define) the locale manually, the code should work on any computer. – stepio Feb 22 '20 at 22:02