Scanner not finding text file
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:
Code Java:
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
Re: Scanner not finding text file
Create a File object (move the new File(filename)) out of the Scanner constructor and print the File class's get absolute path value. That will show you where the computer is looking for the file. Then move the file to that location or change the part to point to where the file is located
Re: Scanner not finding text file
I am obviously doing something wrong on this. When i tried to pull the new file out of the scanner and set it as its own object I got several errors including:
Exception in thread "main" java.lang.UnsupportedOperationException: Not yet implemented
I also get: Cannot find symbol
symbol: method getAbsolutePath()
location: variable newFile of type java.lang.Object
Any help here would be aprreciated.
Re: Scanner not finding text file
Post your code that has the error.