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 14 of 14

Thread: Problem reading in the files

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem reading in the files

    I have this piece of code that is suppose to read in a file and count how many lines it has, number of words, etc... but for some reason it is not reading this file in for some reason

    http://faculty.utpa.edu/rtschweller/...dictionary.txt

    I can put in other inputs and other words, as a matter of fact, I can put what I want and it will read the file properly and do everything i ask it to. I can even give it huge files that take my computer a while to compute and it will still work but once I give it this file, it will not read it at all. I can erase all of it's input and put something else in this file it will read it but I give it the input in the file it won't read it. I even cut this file in half a few times until i gave it a tiny input and it started reading it. I copied it over millions of times and it read it, I give it back this file's input again, it won't even attempt to read it.

    Any suggestions?

    HTML Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package dictionary;
    
    import java.util.*;
    import java.io.*;
    
    /**
     *
     * @author Marlin
     */
    public class Driver {
    
        public static void main(String [] args)
    	{
    		//open input and output files
    		File input, output;
    		input = new File("data.txt");
    		output = new File("pigReport.txt");
    
    		//create a report based on input file, written to output file
    		createReport(input, output);
    	}
    
    	public static void createReport(File input, File output)
    	{
    		try
    		{
    			//create buffered reader and writer objects
    			Scanner reader = new Scanner(input);
    			BufferedWriter writer = new BufferedWriter( new FileWriter(output) );
    
    			//declare some counter variables
    			int linecount=0;
    			int wordcount=0;
    			int charcount=0;
    			boolean workappropriate=true;
    
    			//read each line from input file one by one...
    			while( reader.hasNextLine() )
    			{
    				//increment line count
    				linecount++;
    
    				//get that line from the file
    				String theline = reader.nextLine();
    
    				//increment wordcount by number of words in line
    				wordcount += countWords(theline);
    
    				//increment char count by chars in line
    				charcount += theline.length();
    
    				//check for swear words
    				if( theline.toLowerCase().contains("frack") )
    					workappropriate = false;
    			}
    
    			//write statistics to output file
    			writer.write("The report:\n");
    			writer.write("Number of lines: " + linecount + "\n");
    			writer.write("Number of words: " + wordcount + "\n");
    
    			if( workappropriate )
    				writer.write("This is ok for work");
    			else
    				writer.write("Only when boss is not looking...");
    
    			writer.close();
    
    		}
    		catch(Exception e)
    		{
    			System.out.println("File problem...");
    		}
    
    
    
    	}
    
    	//return number of words in s
    	public static int countWords(String s)
    	{
    		String [] words;
    
    		words = s.split(" ");
    
    		return words.length;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading in the files

    it is not reading this file in for some reason
    Are there any error messages when the program is executed? Copy the full text and paste it here. Add a call to the printStackTrace() method to the catch block to show the full text of the error message.

    If the file you are having problems with is a text file, copy its contents and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading in the files

    It compiles just fine and all but it won't read it in. It won't even throw the exception

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading in the files

    What does the code do when it is executed? Add an if statement inside the while loop to print out a message every 100 lines it reads. Something line this:
      if(lineCount % 100 == 0) System.out.println("lC="+lineCount);
    That will show you what the code in the loop is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading in the files

    It will read files in like it is supposed to. It is suppose to count the lines and words in the document but it doesn't matter what I give it, it works. I would of put the statement you put in cause I do like your tips alot norm but I ran through it step by step and for other entries, even if it is the same file, regardless if they are bigger or smaller or what not, will read in and count like it is suppose to but when I put in this dictionary that I linked in the orginal comment, it won't even try the loop, it just goes, immediately returns false and skips over the loop completely.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading in the files

    I have no idea what is wrong with the working of the program on your system.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading in the files

    Well, at least you tried. I ask my professor the same thing and it just baffles him as much as it does you. I take it you never seen something quite like this? I swear it's crazy and people think I'm making it up but I'm like trust me, it's too stupid for me to make up

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading in the files

    What happens if you read the file in an editor and save it with a different name?
    When I use the program with the file:

    The report:
    Number of lines: 202412
    Number of words: 202412
    Number of chars: 1852943
    This is ok for work
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading in the files

    It opens up normally in any editor and I just tried a different name. Nothing. I tried this on another computer, still the same thing. I'm using netbeans if that helps. I also used different editors and nothing. I still get 0 for everything. I even restarted the system and still nothing

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Problem reading in the files

    I'm using Eclipse and it works fine.

    Where did you put the file and what did you call it?

    Try creating a small data file with a few lines and see if that works.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading in the files

    I'm using netbeans like I said but I could try using eclipse, would that work? I have in the "My Documents" folder my netbeans folder where it then saves all my projects by default in the projects folder. My project "Dictionary" as I simply called it, is listed and when I open it, in the first page you see is where I dropped the file. It appears to read it in and its only with this text, which I need mind you, not trying to be rude, that it decides, nope, I'm not even going to try to read the file. I already tried to modify the file in different ways to see if there is a reason why it isn't doing anything. Different mods and editors don't do anything. I thought it was too big to read so I gave it another file and increased it's size and it read it just fine. I gave it a 100MB file to read in of useless text and it did read it. I even put in frack and it detected it in the text. SO I know it read all 4 millions lines of text. I don't change a thing other than I simply give it this dictionary, nothing. I tried changing the name, nothing

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading in the files

    I don't think using an IDE helps. Try opening a command prompt window and using the java command to execute the program.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading in the files

    Can't say I know how to do that norm. What do you mean using the java command?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem reading in the files

    Your use of IDEs has isolated you from how things work.
    See the tutorial:
    "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading files and writing files
    By ProgrammerNewbie in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2012, 12:13 AM
  2. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 12:47 PM
  3. problem reading from two files into array objects
    By JavaRTnoob in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 30th, 2010, 09:21 AM
  4. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM
  5. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM