Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Scanner not finding text file

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scanner not finding text file

    Post your code that has the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM