I'm working on a project for school but am running into an issue with my code. This program prompts the user to enter the name of a file (the file is called listings.txt and it is a simple text file copied into the source code folder) which is parsed and the data is used to create a couple of other files. My problem is, for some reason the scanner is not able to locate the file. I'm not sure where I'm going wrong with this. Here is the code for the scanner:
package task2parta; //list of imports import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import java.util.Map.Entry; public class Task2partA { /** * @param args the command line arguments */ public static void main(String[] args) { Set<String> listType = new TreeSet<String>(); Map<String, Double> total = new TreeMap<String, Double>(); Scanner cin; //initializes the scanner and prompts the user for the file name cin = new Scanner(System.in); System.out.print("Name of input file: "); String fileName = cin.next(); try { //This code opens the file cin = new Scanner(new File(fileName)); // This code reads through the file line by line while (cin.hasNextLine()) { String line = cin.nextLine(); line = line.replaceAll("\t", " "); do { line = line.replaceAll(" ", " "); } while (line.indexOf(" ") >= 0); String[] arr = line.split(" ");
The only thing I can think of is I am not putting the file in the correct folder. I am using the following location for storing the listings.txt file: C:\Users\User\Documents\NetBeansProjects\Task2part A\src\task2parta