Home » Java » visual studio – Java Code to read file is not compiling on VS code editor-Exceptionshub

visual studio – Java Code to read file is not compiling on VS code editor-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


import java.util.Scanner;
import java.io.*;
 * PROJECT I: ReadData.java
 * A sample code which demonstrates how to read data from an ASCII file using
 * the Scanner

public class ReadData {

  public static void main(String[] args) {

    double x,y,rad;
    int lineCount = 0;

    //Initialise the max and minimum to something sensible
    double maxX = Double.MIN_VALUE;
    double minX = Double.MAX_VALUE;
    double maxY = Double.MIN_VALUE;
    double minY = Double.MAX_VALUE;
 try {
        File file = new File("C:\Users\Yakov\Desktop\Study stuff\MA117\Project 1.data");
      Scanner scanner = new Scanner(new BufferedReader(new FileReader(file)));

      while(scanner.hasNext()) {

        //Read the three valies on each line of the file
        x = scanner.nextDouble();
        y = scanner.nextDouble();
        rad = scanner.nextDouble();

        //Increment the linecounter. 

        // Find max, min for x and y coords
        if (x > maxX) {
          maxX = x;
        if (y > maxY) {
          maxY = y;
        if (x < minX) {
            minX = x;
        if (y < minY) {
          minY = y;

    } catch(Exception e) {
      System.err.println("An error has occured. See below for details");

    System.out.println("Information about the data:");
    System.out.println("  Number of lines in file: " + lineCount);

    System.out.format("  Max x-coord: %10.9f\n", maxX);
    System.out.format("  Min x-coord: %10.9f\n", minX);
    System.out.format("  Max y-coord: %10.9f\n", maxY);
    System.out.format("  Min y-coord: %10.9f\n", minY);



I’m trying to run this Java code in VS code editor. It is a sample code which is supposed to be able to run, which is provided on the internet. How do I make this run? Is there maybe a problem in this code? The file I have inserted in is real( it exists) , so I dont see any reason this code is not running. I am using windows. Any idea how to fix this? The error is “Exception in thread “main” java.lang.Error: Unresolved compilation problem:

    at ReadData.main(ReadData (1).java:24)"

Edit: import of scanner is not the problem

How to&Answers:

Your error is that you have violated the “first rule of Java”:

Every class have to be saved in an file with identical name appended by .java.

Therefore the class ReadData have to be saved in a file named ReadData.java – not ReadData (1).java as you did.


You don’t have an import for Scanner – it’s under java.util and you only have java.io.*