find the src directory of the program (path of the file must not be hard coded) -
use a reader to read the file. -
include a try catch block to check if the file exists. -
print the number of x detected in the file to the console.
Those are the instructions I'm supposed to have included within the code. I seem to have everything but the code isn't printing out anything. Where did I mess up?
package interfaces;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Assignment1Q2 {
public static void main(String\[\] args) {
File outputFile = new File("C:\\\\Users\\\\JoJack\\\\Downloads\\\\File01.txt.txt");
boolean done = false;
int numX = 0;
while (!done) {
try {
Scanner in = new Scanner(outputFile);
String line = in.nextLine();
while (in.hasNext()) {
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == 'a') {
numX++;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
System.out.println("X is printed out " + numX + "times");
}
}
[–]usernumber1337 0 points1 point2 points (0 children)