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

Thread: Not Reading From Text File

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not Reading From Text File

    I am making a program for my Java class it is supposed to give suggestions for jumbles and my Professor wants me to do it this way but for some reason tester1 and tester2 are reading null right off the bat in the first two while loops. Please help!:

    package mysort;
    import java.util.Arrays;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    public class Sorting {
     
    	public static void main (String [] args) throws IOException
    	{
     
    		int i=0;
    		int n=0;
    		int t=0;
    		int r=0;
    		int p=0;
    		int y=0;
    		int q=0;
    		BufferedReader ldj = null;
    		BufferedReader ldc = null;
    		ldj = new BufferedReader(new FileReader("small_j.txt"));
    		ldc = new BufferedReader(new FileReader("small_d.txt"));
    		String tester1;
    		String tester2;
    		tester1 = "1";
    		tester2 = "1";
    		int jl=0;
    		int dl=0;
    		while ((tester1=ldj.readLine()) !=null);
    		{
    			jl++;
    		}
    		while ((tester2=ldc.readLine()) !=null);
    		{
    			dl++;
    		}
    		ldj.close();
    		ldc.close();
    		BufferedReader brj = null;
    		BufferedReader brd = null;
    		brj = new BufferedReader(new FileReader("small_j.txt"));
    		brd = new BufferedReader(new FileReader("small_d.txt"));
    		String [] jumbles = new String [jl];
    		String [] dictionary = new String [dl];
     
    		for (t=0;t<jl;t++) 
    		{
    			jumbles [t] =brj.readLine();
    		}
    		for (y=0;y<dl;y++)
    		{
    			dictionary [y] = brd.readLine();
    		}
    		String dictionary_copy[]=Arrays.copyOf(dictionary,dl);
    		String [] jumble_copy = Arrays.copyOf(jumbles, jl);
    		for(n=0;n<dl;n++)
    		{
    			dictionary_copy[n]=sortElement(dictionary_copy[n]);
    		}
     
    		for(p=0;p<jl;p++)
    		{
    			jumbles[p] = sortElement(jumbles[p]);
    		}
     
    		for(q=0;q<jl;q++)
    		{
    			r = 0;
    			System.out.println(jumble_copy[q]+" possible matches:");
    			for(r=0;r<dl;r++)
    			{
    				if(jumbles[q]==dictionary_copy[r])
    				{
    					System.out.println(dictionary[r]);
    				}
    			}
    		}
     
     
     
    	}
    	public static String sortElement(String d)
    	{
    		int b=0;
    		int n=d.length();
    		char [] l = d.toCharArray();
    		Arrays.sort(l);
    		d="";
    		for (b=0;b<n;b++)
    		{
    			d=d+l[b];
    		}
    		return d;
    	}
    }
    Last edited by JavaLaxer15; March 18th, 2012 at 09:56 AM.


  2. #2
    Junior Member
    Join Date
    Aug 2010
    Location
    UK
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Not Reading From Text File

    When ever I get these sorts of problems I first check the following:

    -Do the files exist ?
    -Have I got the right case/spelling for the file names?
    -Do the files have the correct data in them?
    -Is the extension correct in the program (e.g .txt)?
    -Are the files in the right location on your system?


    **Another issue could be the IDE that you're using - are you using one btw?

Similar Threads

  1. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  2. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM

Tags for this Thread