import java.util.*;
import java.io.*;
public class testing
{
	public static void main(String[]args)
	{
		int NoOfLines=0;
		int i=0; 
		String fileName = "tingtings.txt";
		String line;
		String[] lines = new String[1000];
		try
		{
			BufferedReader brStream1 = new BufferedReader(new FileReader(fileName));
			while ((line = brStream1.readLine())!= null)
			{
				NoOfLines++;

			}
			while ((line = brStream1.readLine())!= null)
			{
				StringTokenizer aString=new StringTokenizer(line);
				for(i=0; i<lines.length;i++)
				{
				lines[i]=aString.nextToken();
				if (lines[i]==lines[i-1])
				{
					NoOfLines--;
					i--;
				}
				}
			}
			brStream1.close();
			print(lines);
			System.out.println(NoOfLines);
		}

		catch (IOException e)
		{	
			System.out.println("ERROR");
		}
		
	}
		public static void print(String[] line) 
		{
        for(int i = 0; i < line.length; i++)
            System.out.println(line[i]);
		}
}

when i run the program, it gives me null instead of the first token from each line. anyone has any idea why this is happening?

Edited 7 Years Ago by Watti: n/a

I think you are reading the file in your first while

while ((line = brStream1.readLine())!= null)
	{
		NoOfLines++;
	}

and there's nothing to read in the second while because you have reached the end of file

Anyway the second while it's a little messed up for what you are trying to do, if this is to put the firs token of each line in the lines array

I think you are reading the file in your first while

while ((line = brStream1.readLine())!= null)
	{
		NoOfLines++;
	}

and there's nothing to read in the second while because you have reached the end of file

Anyway the second while it's a little messed up for what you are trying to do, if this is to put the firs token of each line in the lines array

Oh, the file contains repeats.
For example
TKGS - Tanjong Katong Girls' School
TP - Temasek Polytechnic
TP - Traffic Police
and i'm only supposed store TP once
that was what i was trying to do.
but it's not working apparently :l

public class testing{
	
	// declared as static member so it can be used in print method
	static int arraySize=0;
	
	public static void main(String[]args)
	{
		int NoOfLines = 0;
		int i=0; 
		String fileName = "tingtings.txt";
		String line;
		String[] lines = new String[1000];
		try
		{
			BufferedReader brStream1 = new BufferedReader(new FileReader(fileName));
			/*
				while ((line = brStream1.readLine())!= null)
				{
					NoOfLines++;

				}*/
			while ((line = brStream1.readLine())!= null)
			{
				NoOfLines++;
				StringTokenizer aString = new StringTokenizer(line);
				// get first token from line
				String str = aString.nextToken();
				
				// search if the string was entered before
				boolean alreadyIn = false;
				for(i=0; i<arraySize; i++)
				{					
		// use equals to compare strings, not ==
					if (lines[i].equals(str))
					{
						alreadyIn = true;
					}
				}
				// add to the end of array if not in already
				if (alreadyIn == false){
					lines[arraySize] = str;
					arraySize++;
				}
			}
			
			brStream1.close();
			print(lines);
			System.out.println(NoOfLines);
		}

		catch (IOException e)
		{	
			e.printStackTrace();
			System.out.println("ERROR");
		}
			
	}
		
	public static void print(String[] line) 
	{
		for(int i = 0; i < arraySize; i++)
			System.out.println(line[i]);
	}
}

this should do the work

GL!

This article has been dead for over six months. Start a new discussion instead.