I have this code but when I run it, it can not read the file and I get this error:
Exception in thread "main" java.io.FileNotFoundException: PBM.txt (The system cannot find the file specified)
is anybody can help me which part of the code has problem? I made a file with the name:"PBM" in my package, but it does not work!
here is my code:
import java.util.Scanner;
import java.io.*;
public class PBM {
private int[][] bits;
private int rows, columns;
PBM() throws IOException {
Scanner PBM = new Scanner(new File("PBM.txt"));
if (!PBM.next().equals("P1"))
throw new IOException("Format error");
columns = PBM.nextInt();
rows = PBM.nextInt();
bits = new int[rows][columns];
for (int i = 0; i < rows; i++) {
String line = PBM.next();
for (int j = 0; j < columns; j++)
bits[i][j] = line.charAt(j) - '0';
}
}
public String toString() {
String result = "";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (bits[i][j] == 1)
result += "*";
else
result += " ";
}
result += "\n";
}
return result;
}
public static void main(String a[]) throws IOException {
PBM Ob = new PBM();
System.out.println(Ob);
}
}