1

before anyone tells me, i checked the path, and i tried both with abosolute and relative paths, but the error remains the same. Here is the code.

public class Main {
    public static void main (String[] args) {
      Scanner input = new Scanner(System.in);
      Scanner gimmiethetxtplz = new Scanner(new File("cars.txt"));
      int rows = input.nextInt();
      int columns = input.nextInt();
      String[][] cars = new String[rows][columns];
    
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
             cars[i][j] = gimmiethetxtplz.nextLine();
             System.out.print(cars[i][j] + " ");
                     }
               System.out.println();
              }
           input.close();
        }
     }

The file is in the src folder

Exception in thread "main" java.lang.Error: Unresolved compilation 
problem: 
Unhandled exception type FileNotFoundException

at training.Main.main(Main.java:9)
  • 2
    1. Print the result of `new File("cars.txt").getAbsolutePath()` to verify that it tries to read what you think it tries. 2. Verify that a file with that name actually exists where you think it does. Specifically if you use Windows ensure that Explorer shows file extensions, because you might actually have a file called `cars.txt.txt` and not know it because Explorer hides the (last) file extension by default. 3. When asking for help *always* provide the *full* stack trace of the exception that you get, don't just describe it. 4. shows us how you tried to use the absolute path. – Joachim Sauer Jun 27 '22 at 08:54
  • 1
    `new File("cars.txt")` Java will assume that file _cars.txt_ is located in the [working directory](https://stackoverflow.com/questions/4871051/how-to-get-the-current-working-directory-in-java). – Abra Jun 27 '22 at 09:04
  • i'm on linux therefore i don't have the problem that it might be hiding the extensions. Then, with getAbsolutePath() i verified that indeed the file is what i think it is, and it is where i think it is. Only when i call it with the scanner it tells me it couldn't find it. And the full exception is FileNotFoundException, i didn't get anything else, so i don't even know what details i can provide more. – Costantino Tessera Jun 27 '22 at 09:05
  • I wrote "full stack trace of the exception". If any of those words mean nothing to you, then please google what they could mean and don't just repeat information you've already given us. As it stands your question doesn't contain enough information to help you. Is `FileNotFoundException` **literally** the only output that you get? I find that hard to believe. How **exactly** are you launching your code ([edit] your question to include the command and/or screenshot of dialog, if through UI). – Joachim Sauer Jun 27 '22 at 10:14
  • Take a closer look at the message - specifically, "Unresolved compilation problem". – vsfDawg Jun 27 '22 at 11:24
  • 1
    Just for the record: I hate the fact that Eclipse *still* silently runs code that doesn't actually compile which leads to questions like this *again and again*. If your code doesn't compile, don#t just click through the "the code is broken, run it anyway?" dialog, but **fix your code first** and if you don't know how, then **post a question here asking about the error you get in your IDE** (after googling it, of course). But that default setting of Eclipse has wasted so many lifetimes of developers, for basically no gain. – Joachim Sauer Jun 27 '22 at 11:40
  • 1
    @JoachimSauer IKR i first started coding with eclipse and i didnt realize until i switched to other ide's how terrible it actually was – Aniketh Malyala Jun 27 '22 at 12:16

1 Answers1

1

The problem is not with where your file is, but it is just that you need to include something in your main method before reading files. As another user already mentioned, take a closer look at your error and it will reveal that the error is a compilation error.

When defining your main method, instead of doing:

public static void main (String[] args) {

Make sure you write:

public static void main (String[] args) throws IOExcpetion{

This will basically let your compiler know you are aware of the exception since it is mandatory to check for it.

Overall your code should look something like this:

import java.io.*;
import java.util.*;
public class Main {
    public static void main (String[] args) throws IOException{
      Scanner input = new Scanner(System.in);
      Scanner gimmiethetxtplz = new Scanner(new File("cars.txt"));
      int rows = input.nextInt();
      int columns = input.nextInt();
      String[][] cars = new String[rows][columns];
    
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
             cars[i][j] = gimmiethetxtplz.next();
             System.out.print(cars[i][j] + " ");
                     }
               System.out.println();
              }
           input.close();
        }
     }

Note: I edited part of your for loop to say .next() instead of .nextLine(), because otherwise each element of your cars array would be an entire line instead of a single letter, and you would then get an error when reading your file.

Sample Input from Console:

3
3

Sample File (cars.txt):

a b c
d e f
g h i

Output:

a b c 
d e f 
g h i 

Let me know if you have any further questions or clarifications! :)

Aniketh Malyala
  • 2,650
  • 1
  • 5
  • 14