Hi there, first post!

First off, I apologise for asking such a basic question, but I am new to Java (done VB before..) and was wondering if you could please help me out with a little problem I'm having!

I have a method that reads data from a text file, uses a StringTokenizer, and puts the information in to an ArrayList called StudentArray

public void readFile()
	{
		ArrayList<String> studentArray = new ArrayList<String>();		//Declares new ArrayList to contain string values
		try
		{
			BufferedReader in = new BufferedReader(new FileReader("/Users/John/Resources/Teech/studentList.txt"));	//New bufferedReader and fileReader
			String theText = in.readLine();		//Get next line from file
			
			do
			{				
				StringTokenizer st = new StringTokenizer(theText,","); 		//Splits the text by comma
				int tokenNum = 0;		//Delcares tokenNum variable, initialised at zero
				do
				{
					String thisToken = st.nextToken();		//Sets current token as the next token in the file

					System.out.println("Token "+tokenNum+": "+thisToken);	//Prints out current token information to terminal
										
					studentArray.add(tokenNum, thisToken);		//Adds thisToken to the ArrayList at position tokenNum
					
					System.out.println("The content of studentArray is: " + studentArray);			//Prints out the current contents of studentList
				    System.out.println("The size of an studentArray is: " + studentArray.size());	 	//Prints out the current size of studentList
				    
					tokenNum++;		//Increments tokenNum for next entry
				} 
				while (st.hasMoreTokens());

				theText = in.readLine(); 		//Get next line from file
				System.out.println(theText);	
			} 
			while (theText != null);			    	
		}
		// Catches any error conditions
		catch (IOException e)
		{
			System.err.println ("Unable to read from file");
			System.exit(-1);
		}
	}

What I wish to do is now create a method to do a simple linear search of this array, so I created a separate method called linearSearch() and tried the following code

public void linearSearch()
	{
		String searchTerm = txtsearch.getText();
		int notFound = -1;
		int counter;
		
		for(counter = 0; counter<studentArray.length; counter++)
		{
			if(searchTerm == studentArray[counter])
			{
				return counter;
			}
			else
			{
				return notFound;
			}
		}
	}

Now, I'm pretty sure my logic is right for the search, and the readFile method functions as intended, but I always get an error when trying to use the studentArray. How would I utilise studentArray in this method, by using its contents from the method above? I've not done a lot of parameter passing so any help would be appreciated.

Many thanks for your time.

Recommended Answers

All 2 Replies

Move your declaration of ArrayList<String> studentArray up to the class level, instead of within your readFile() method.

On a side note, you will have some problems with your linearSearch() as it has void as a return type. Change it to int and you'll be fine :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.